[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$f1x3mi7znzjgsp":3},{"_id":4,"slug":5,"title":6,"subtitle":7,"kind":8,"cards":9,"tags":33,"categories":35,"source":37,"lang":40,"author":41,"audioState":44,"stats":45,"publishedAt":48,"renderer":49},"6abc2d6fca21c797c7e9e6e3","linq-aggregate-operators-beyond-sum-and-count-1c6bd3e3","LINQ Aggregate Operators: Beyond Sum and Count","Everyone knows Count() and Sum().","news",[10,13,18,23,28],{"headline":6,"body":11,"imageUrl":12,"sourceImageUrl":12},"Everyone knows Count() and Sum(). But LINQ's aggregate family goes deeper — and the general-purpose Aggregate() operator can implement any of them, plus custom reductions you haven't imagined yet. Let's explore what's beyond the basics. These translate to SQL aggregates — executed in the database, not in memory. Efficient. What happens when the sequence is empty? Sum and Count handle emptiness gracefully. The others throw. The fix: use nullable overloads:","https:\u002F\u002Fmedia2.dev.to\u002Fdynamic\u002Fimage\u002Fwidth=1200,height=627,fit=cover,gravity=auto,format=auto\u002Fhttps%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Febbw1w01fnjkp6hs1utg.png",{"headline":14,"body":15,"imageUrl":16,"images":17},"Fun fact: This null behavior matches SQL. SELECT","Fun fact: This null behavior matches SQL. SELECT AVG(Price) FROM Products WHERE 1=0 returns NULL, not zero. LINQ chose to throw exceptions because returning a misleading default (like 0 for average) could hide bugs. The nullable overloads give you SQL semantics when you want them. MinBy and MaxBy: The Element, Not the Value New in .NET 6, these return the element with the min\u002Fmax value, not the value itself: MinBy\u002FMaxBy are cleaner and more efficient. In-memory, it's one iteration instead of two. Note: these are LINQ to Objects only — EF Core doesn't translate them to SQL. Use OrderBy().First() for database: The General-Purpose Aggregate Aggregate() is the Swiss Army knife.","\u002Fapi\u002Fmedia\u002Fposts\u002Flinq-aggregate-operators-beyond-sum-and-count-1c6bd3e3\u002F1.webp",{"local":16},{"headline":19,"body":20,"imageUrl":21,"images":22},"It takes: A function that combines accumulator +","It takes: A function that combines accumulator + element (Optionally) a result selector Build any aggregation you want: Real-World Aggregate Patterns Aggregate vs GroupBy for Summaries Need multiple aggregates by group? GroupBy with Select: This translates to efficient SQL with GROUP BY and aggregate functions. Count() returns int. If you have more than 2.1 billion items (rare, but possible): Database vs Memory Aggregation When aggregating on IQueryable, EF executes in the database: If you materialize first, aggregation happens in memory: For large datasets, database aggregation is dramatically faster. Always aggregate before .ToList() when possible. Aggregate in database — call Sum\u002FCount on IQueryable, not after ToList() Watch for nulls — use nullable overloads for Min\u002FMax\u002FAverage on possibly-empty sequences Need the element?","\u002Fapi\u002Fmedia\u002Fposts\u002Flinq-aggregate-operators-beyond-sum-and-count-1c6bd3e3\u002F2.webp",{"local":21},{"headline":24,"body":25,"imageUrl":26,"images":27},"Use MinBy\u002FMaxBy (.NET 6+) or OrderBy().First() Custom reduction?","Use MinBy\u002FMaxBy (.NET 6+) or OrderBy().First() Custom reduction? Aggregate() can implement anything Per-group stats? GroupBy + aggregate functions","\u002Fapi\u002Fmedia\u002Fposts\u002Flinq-aggregate-operators-beyond-sum-and-count-1c6bd3e3\u002F3.webp",{"local":26},{"headline":29,"body":30,"imageUrl":31,"images":32},"Next time, we'll tackle EF Core's raw SQL","Next time, we'll tackle EF Core's raw SQL escape hatch — FromSqlRaw, ExecuteSqlRaw, and when to break out of LINQ entirely. Hope to see you! For further actions, you may consider blocking this person and\u002For reporting abuse","\u002Fapi\u002Fmedia\u002Fposts\u002Flinq-aggregate-operators-beyond-sum-and-count-1c6bd3e3\u002F4.webp",{"local":31},[34],"dev",[36],"Technology",{"name":38,"url":39},"Dev.to","https:\u002F\u002Fdev.to\u002Fhomolibere\u002Flinq-aggregate-operators-beyond-sum-and-count-8l7","en",{"handle":42,"displayName":43},"spots","Spots","queued",{"views":46,"likes":47,"saves":47,"shares":47,"completions":47,"opens":47,"skips":47,"depthSum":47},3,0,"2026-09-29T21:28:15.022Z","local"]