My understanding of expression tree is :
Expression trees are in-memory representation of expression like arithmetic or boolean expression.The expressions are stored into the parsed tree.so we can easily transalate into any other language.
Linq to SQL
uses expression tree.Normally in LINQ to SQL
query the compiler translates it into parsed expression trees.These are passed to Sql Server as T-SQL Statements.The Sql server executes the T-SQL query and sends down the result back.That is why when you execute LINQ to SQL
you gets IQueryable<T>
not IEnumetrable<T>
.Because IQuerybale
contains
public IQueryable:IEnumerable
{
Type Element {get;}
Expression Expression {get;}
IQueryaleProvider Provider {get;}
}
Questions :
Microsoft uses Expression trees to play with LINQ-to-Sql.What are the different ways can i use expression trees to boost my code.
Apart from LINQ to SQL,Linq to amazon ,who used expression trees in their applications?
Linq to Object return IEnumerable,Linq to SQL return IQueryable ,What does LINQ to XML return?
Expression trees offer a way to inspect some piece of code before it is actually compiled, at runtime.
You can do exactly two things with an expression tree:
Expression trees are cool, but the odds of them actually having any direct usefulness for a typical website or enterprise app are remote. You would only use them when you (a) want to generate a different type of code from C# source at runtime, or (b) want to use Reflection but provide some measure of compile-time safety.
If you are working on reusable libraries - DI frameworks, AOP frameworks, MVC/MVP/MVVM frameworks, ORM frameworks, and so on, or trying to extend one of these frameworks (including Linq-to-XYZ), then you will likely find a use for expression trees (such as the Linq to SQL batch update extension), although it's hard to discuss specific examples because it would depend entirely on the design of such a framework.
If you're not building these sorts of tools then expression trees aren't much more than a curiosity. Great to learn about of course - but don't be disappointed if you don't find too many practical uses for them.
A few examples of libraries that use expression trees:
...That should give you an idea of the kind of projects that make serious use of expression trees. So if you're building your own runtime environment or MVC, you definitely want to learn everything you can. ;)
IEnumerable<XElement>
s.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With