LINQ Aggregate Operator: You Don’t Need It Until You Need It!

By April 30, 2014 .NET No Comments

Lately I write so many LINQ queries on an almost daily basis that it is easy to think I have used every kind of operator in one way or another. It’s kind of fun, really – trying to come up with the most efficient query to get what I want out of a collection of items, for example.  But even when I have been using it for a while, LINQ can still surprise me!

I have used the other aggregation operators repeatedly, and they are perfect for my typical aggregation needs:  sum, count, average, min, and max.  But this Aggregate operator was a different story.  I had never really figured out what it did, much less a need to use it.

It turns out to be one of those operators you don’t know you need…until you do.  You can do any custom type of aggregation using this operator. So that is nice for any aggregation that isn’t covered by the other operators, for example.  And you can use a seed value to start your aggregation.  But what I like is how you can use the current value of the aggregation in the aggregate itself! 

To see what I mean, look at the following example:  A set number of items are available, but there are multiple orders requested for those items.  We want to see how many items are accounted for without exceeding the available count.  So, we need to use the current value of accounted for items, or CurrentTotal in this case, to check against before adding in the next request.

If you use LINQPad (you do use LINQPad, right?) you can copy this example directly into a query, set your Language to C# Program, and run it yourself. 

void Main()
var items
= new List<SampleOrder> {
new SampleOrder { Name = "One", Requested=4 },
new SampleOrder { Name = "Two", Requested=7 },
new SampleOrder { Name = "Three", Requested=9 },
new SampleOrder { Name = "Four", Requested=3 }
var AvailableItems
= 15;

double ItemsSold = items.Aggregate(0, (currentTotal, nextItem) =>
<= AvailableItems - nextItem.Requested ? currentTotal + nextItem.Requested : currentTotal);


// Define other methods and classes here
class SampleOrder {
public string Name {get;set;}
public int Requested {get;set;}

With 15 available items, we get a result of 14, which means requests One, Two, and Four can be filled, but not Three.  You can change the number of available and requested items and see for yourself how it works.


I would love to hear your uses and experiences with LINQ aggregates in the comments!

Did you learn something? I really recommend you attend FalafelCON 2014, where there will be many chances to learn new development techniques. Let me know you’re coming on Twitter!

The following two tabs change content below.
Rachel finds joy in meeting challenges in her career and her life. After deciding to pursue a career in electronics, and as one of only a handful of female students in her department, Rachel graduated at the top of her class with a BS in Electrical and Computer Engineering from Oklahoma State University. Two years after graduation, she was leading the software design team on a multi-million dollar testing system for the Department of Energy. Rachel has experience in circuitry design and automated test systems, but her true passion has always been for software. Rachel loves to spend time with her husband and daughter on their Oklahoma cattle ranch enjoying horseback riding, taking photos, and making memories with her extended family.