How does LINQ to SQL know what's inside a delegate?

Pablo Honey

With entity framework, we can do :

MyContext context = ... // a normal EF context 

var products =  context.Products.Where(p => p.Location == "France") ; 

or

var products =  context.Products.Where(p => p.CategoryId == 54) ;

Which are both translate in their equivalent SQL query.

OK, but somewhere over there, there's a piece of code that handle this :

public static IEnumerable<T> Where(Func<bool, T> func) {
     ......
}

From that Where function, how do LINQ to SQL know what's the implementation of func ?

Obvious answer maybe, but I can't really find it.

xanatos

You should really do a Go To Definition on your code. The function used for LINQ-to-SQL and for Entity Framework is

IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)

contained in the System.Linq.Queryable that uses expression trees, that are a constuct able to "describe" pieces of code. A little quote:

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

For example, your first expression

var products = context.Products.Where(p => p.Location == "France"); 

is converted by the C# compiler to this code:

ParameterExpression par = Expression.Parameter(typeof(Product), "p");
LambdaExpression lambda = Expression.Lambda(
    Expression.Equal(
        Expression.Property(par, "Location"), 
        Expression.Constant("France")), 
    par);

var products = context.Products.Where(lambda); 

Now... while the creation of an expression tree is quite simple, the reverse operation (de-building an expression tree and creating a query) is something VERY VERY complex. Big big headache complex. Nearly magic-level complex :-)

The problem isn't de-building the expression tree. That is easy. You use an ExpressionVisitor and you are done. The problem is merging the various layers of LINQ query and comprehending what the programmer wanted to obtain.

I'll add that IL (the "assembly" of .NET) is high level enough that it is possible to decompile it (see for example IlSpy). There is at least a library, DelegateDecompiler that is able to decompile a delegate to an Expression Tree, so even without Expression Trees, LINQ-to-SQL and EF could have used something similar and decompiled methods directly to SQL language.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does glDrawArrays know what to draw?

From Dev

How does the VertexBuffer know the type of struct inside it?

From Dev

How does a delegate remember it's parameters?

From Dev

How does this class know what table to query?

From Dev

How does SQL logic in this php 'know' what to select?

From Dev

Linq lambda not working but delegate does

From Dev

How to know which Linq statement produced the SQL on hand during runtime?

From Dev

How does python "know" what to do with the "in" keyword?

From Dev

How does Generation Type IDENTITY work? How does it know what's the next primary key to insert

From Dev

How does rowfun know to reference variables inside a table

From Dev

How does a program know what type a variable is?

From Dev

Does LINQ know how to optimize "queries"?

From Dev

How can I test my web api Post Web method to know what's going on inside?

From Dev

How does it not know what AppDelegate is?

From Dev

How does a shell know home(s)?

From Dev

How does a delegate remember it's parameters?

From Dev

how to delegate evaluation of multiple columns to database server in linq to sql

From Dev

How does SQL logic in this php 'know' what to select?

From Dev

Linq lambda not working but delegate does

From Dev

how does $.post know what data to return?

From Dev

Android: How to allow sharing and know inside the app what was shared

From Dev

How to know which Linq statement produced the SQL on hand during runtime?

From Dev

How does rowfun know to reference variables inside a table

From Dev

How does NSDate seem to know it's TimeZone?

From Dev

How can I test my web api Post Web method to know what's going on inside?

From Dev

How does it not know what AppDelegate is?

From Dev

in Xcode, how does the delegating object know WHICH method on the delegate object to call?

From Dev

How to know what does the variable in SYSTSIN contains?

From Dev

How does Heap know what to sort by?

Related Related

  1. 1

    How does glDrawArrays know what to draw?

  2. 2

    How does the VertexBuffer know the type of struct inside it?

  3. 3

    How does a delegate remember it's parameters?

  4. 4

    How does this class know what table to query?

  5. 5

    How does SQL logic in this php 'know' what to select?

  6. 6

    Linq lambda not working but delegate does

  7. 7

    How to know which Linq statement produced the SQL on hand during runtime?

  8. 8

    How does python "know" what to do with the "in" keyword?

  9. 9

    How does Generation Type IDENTITY work? How does it know what's the next primary key to insert

  10. 10

    How does rowfun know to reference variables inside a table

  11. 11

    How does a program know what type a variable is?

  12. 12

    Does LINQ know how to optimize "queries"?

  13. 13

    How can I test my web api Post Web method to know what's going on inside?

  14. 14

    How does it not know what AppDelegate is?

  15. 15

    How does a shell know home(s)?

  16. 16

    How does a delegate remember it's parameters?

  17. 17

    how to delegate evaluation of multiple columns to database server in linq to sql

  18. 18

    How does SQL logic in this php 'know' what to select?

  19. 19

    Linq lambda not working but delegate does

  20. 20

    how does $.post know what data to return?

  21. 21

    Android: How to allow sharing and know inside the app what was shared

  22. 22

    How to know which Linq statement produced the SQL on hand during runtime?

  23. 23

    How does rowfun know to reference variables inside a table

  24. 24

    How does NSDate seem to know it's TimeZone?

  25. 25

    How can I test my web api Post Web method to know what's going on inside?

  26. 26

    How does it not know what AppDelegate is?

  27. 27

    in Xcode, how does the delegating object know WHICH method on the delegate object to call?

  28. 28

    How to know what does the variable in SYSTSIN contains?

  29. 29

    How does Heap know what to sort by?

HotTag

Archive