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

With an entity infrastructure, we can:

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 both translate in the equivalent SQL query.

OK, but somewhere there is a piece of code that processes this:

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

From this function Where, how do you LINQ to SQLknow what an implementation is func?

The obvious answer may be, but I can not find it.

+4
source share
1 answer

On your code, you really need to move on to the definition. The function used for LINQ-to-SQL and for the Entity Framework,

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

System.Linq.Queryable, , , "" . :

, node , , x < .

,

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

# :

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); 

... , ( ) - . . :-)

- . . ExpressionVisitor, . LINQ , .

, IL ( "".NET) , (., , IlSpy). , , DelegateDecompiler, , LINQ-to -SQL EF - SQL.

+6

Source: https://habr.com/ru/post/1583753/


All Articles