[Solved] Can someone help me find the bottleneck in this code?


That’s because allLogs is lazy in nature — it will actually hit the database when first enumerated through in the foreach (see “Deferred query execution” here). If you materialize the collection before hand like the following, you will see that it steps through the foreach quickly:

db.StatusLogs
    .Where(sl => sl.ID.Value.Equals(foo))
    .GroupBy(sl=>sl.bar)
    .ToList();  // <-- pull the query results into memory

3

solved Can someone help me find the bottleneck in this code?