0

I have the following IQueryable where, with multiple evaluations, but I'm noticing that this resulted in 9 Queries getting executed against the server? When in reality it should only be one?

var datafound = db.CustomersData
    .Where(x => x.EntryDate == eod.EntryDate);

if (!string.IsNullOrWhiteSpace(eod.Type))
    datafound = datafound.Where(x => x.Type == eod.Type);

if (!string.IsNullOrWhiteSpace(eod.LastName))
    datafound = datafound.Where(x => x.LastName.Contains(eod.LastName));

if (!string.IsNullOrWhiteSpace(eod.Address))
    datafound = datafound.Where(x => x.Address.Contains(eod.Address));

if (!string.IsNullOrWhiteSpace(eod.BuildingName))
    datafound = datafound.Where(x => x.BuildingName.Contains(eod.BuildingName));

    .....
    .....


return datafound.ToList();
Antonio Z
  • 11
  • 3
  • 3
    How did you come to the conclusion this resulted in 9 queries? – Travis J Aug 20 '15 at 23:26
  • @TravisJ I agree - I can't see how this would execute 9 times, even if `CustomersData` wasn't an `IQueryable`. Can we see the queries that it is running? – Rob Aug 20 '15 at 23:28
  • Not with `IQueriable`. If yours "..." doesn't hide any additional logic. `ToList()` will "execute" (if I can say so) your actual query. – Andrew Orlov Aug 20 '15 at 23:58
  • It should be one query, unless you add breakpoint to debug. – daniel Aug 21 '15 at 00:58
  • The ... is the same style of query builder. i left out because it repeats the same thing – Antonio Z Aug 21 '15 at 01:05
  • This is what I'm seeing as well when the above logic runs. http://stackoverflow.com/questions/18773386/glimpse-shows-duplicate-ef-queries – Antonio Z Aug 21 '15 at 01:30

1 Answers1

0

The logic had a fillinViewBag Method before the .ToList() and inside fillinViewBag there were several evaluations for .sum .avg etc.

The fillinViewBag Method just needed the .Tolist() before and not after, that made the execution of the Query only 1 and fillinViewBag method just worked with the data from memory.

Thanks for the Quick Feedback.

Antonio Z
  • 11
  • 3