LINQ Interview Question #127
What is the difference between Aggregate and Sum?
Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced
Quick Interview Answer
Sum is a specialized aggregation operator designed specifically for numeric addition. Aggregate is more general and lets the developer define how values are accumulated.
Detailed Explanation
Sum is a specialized aggregation operator designed specifically for numeric addition. Aggregate is more general and lets the developer define how values are accumulated.
Prefer Sum when calculating a total because it communicates intent clearly. Use Aggregate for custom accumulation logic that is not naturally represented by a dedicated LINQ operator.
Code Example
decimal total = orders.Sum(o => o.Total);
string csv = names.Aggregate(
(current, next) => current + ", " + next);