简体   繁体   中英

Selecting and filtering data using Entity Framework and LINQ

Suppose I have the following query:

 context.Orders.Where(predicate).Select(x => new { propA = x.PropA, propB = x.PropB}).ToList();

Does this perform both the where and select functions at the DB/ORM level, returning only the data that satisfy both expressions, or would it return all the results that satisfy the predicate and then perform the select on those?

Thanks.

You are using IQueryable.Select method. So it is definitely translated into SQL.From the docs

The Select<TSource, TResult>(IQueryable<TSource>, Expression<Func<TSource, TResult>>) method generates a MethodCallExpression that represents calling Select<TSource, TResult>(IQueryable<TSource>, Expression<Func<TSource, TResult>> ) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

You can verify this by using a profiler.Or, try calling an unsupported method in the Select and you will get an exception that says linq to entities does not recognize the method X , that also verifies the Select is converted to SQL and not executed in the memory.

It will do both at the DB/ORM level. Results of your query fetched only when you enumerating your IQueriable eg calling ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM