Search code examples
c#entity-frameworkentity-framework-4

is my linq query pulling just what it needs or everything?


I do something like this:

var src = dbContext.Set<Person>().Where(o => o.LastName.StartsWith(search));
var page = src.OrderBy(u => u.Id).Skip((page - 1) * pageSize).Take(pageSize);
var count = src.Count();

is ef pulling from the database everything and after does the query or not ? how do I know this? what are the ways finding out this?

(using ef4 ctp5 code first)


Solution

  • None of the calls you are using is a To* operator (like ToList), so all method calls will be translated into SQL and executed in the database. However, the Count operator is immediately evaluated, you might should delay that assignment to the place you actually need the value. The other variables will be evaluated once iterated over.