Why does LINQ GroupBy behave differently in memory vs database execution?
Updated May 16, 2026
Short answer
In-memory GroupBy uses hash-based grouping, while database GroupBy translates into SQL aggregation.
Deep explanation
LINQ-to-Objects performs grouping using in-memory hash tables, iterating through each element once. In contrast, LINQ-to-Entities translates GroupBy into SQL GROUP BY clauses, which are executed by the database engine using its own aggregation algorithms. Differences arise in performance, supported aggregate functions, and grouping key complexity. Some complex groupings may not translate and fall back to client evaluation.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro