In the Entity Framework, if I do the following:
var count = myIQueryable.Count();
var list = myIQueryable.ToList();
does that hit the Database twice? or just once?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In order to effectively count the number of entries, the framework needs to evaluate the query, thus hitting the Database. However, because the query may have changed between the Count() and ToList(), it must evaluate again. Consider the following:
Put another way, all calls against an IQueryable are still subject to the way it runs its queries. After capturing a query into a List, you are exclusively dealing with your in-memory List, independant of changes that occur to the IQueryable’s data source.