What is the difference between these two LINQ statements?

Posted by jamone on Stack Overflow See other posts from Stack Overflow or by jamone
Published on 2010-04-21T13:24:37Z Indexed on 2010/04/21 13:33 UTC
Read the original article Hit count: 257

Filed under:
|
|

I had the 1nd statement in my code and found it not giving an accurate count, it was returning 1 when the correct answer is 18. To try and debug the problem I broke it out creating the 2nd statement here and the count returns 18. I just don't see what the difference is between these two. It seems like the 1st is just more compact.

I'm currently running these two statements back to back and I'm sure that the database isn't changing between the two.

int count = (from s in surveysThisQuarter
             where s.FacilityID == facility.LocationID
             select s.Deficiencies).Count();

vs

 var tempSurveys = from s in surveysThisQuarter
                   where s.FacilityID == facility.LocationID
                   select s;
 int count = 0;
 foreach (Survey s in tempSurveys)
     count += s.Deficiencies.Count();

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ