0

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.

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.