Practical .NET

Grouping in LINQ with Methods

Peter follows up -- yet again – on a column on how to group results with LINQ using the SQL-like syntax with the same solution using the method-based syntax. And, no, you're not seeing double.

A little while back, I did a column on how to group results in LINQ to handle problems like "Your users want to see the 10 customers who have bought the most from you along with their related sales orders." As I pointed out, this isn't an unusual query: Sometimes users don't want the record: they want a group of records that meet a condition. That's were the ability of LINQ to create groups of objects that share a common value is useful -- for this problem, for example, you want to group all the salesorders that share a CustomerId.

In the original column, I used the SQL-like syntax and got some feedback from readers who wanted to see the method-based syntax (and, yes, this is different from the last column based on feedback from readers). So that's what this tip shows. As in the column, I'll do both Visual Basic and C#. However, for the method-based syntax, that isn't really necessary because the two solutions are almost identical in the two languages.

In the method-based syntax, you use the GroupBy method passing a lambda expression that returns the property you want to group by. Here's the C# code: var OrdersByCustomer = db.Salesorders.GroupBy(so => so.CustomerId). And here's the Visual Basic code:

Dim OrdersByCustomer = db.Salesorders.GroupBy(Function(so) so.CustomerId).

The GroupBy method returns a collection of IGrouping objects so, in the subsequent method calls, it's a collection of IGrouping objects that you'll be working with. An IGrouping object is a collection of objects (of SalesOrder objects, in this case). Unlike other collections, it has an additional property called Key that holds the shared value (in this case, that's the CustomerId that all the SalesOrders share).

Just to demonstrate that an IGrouping object can be treated like any other collection, in the following LINQ statement, I've thrown in a Where method to select only those collections that have at least five SalesOrders in them by checking the Count of items in each IGrouping collection. Presumably, this weeds out "customers we're not interested in" because they don't do enough business with us. Here's the Visual Basic code:

Dim OrdersByCustomer = db.Salesorders.GroupBy(Function(so) so.CustomerId).
                                      Where(Function(tos) tos.Count() > 5).

And here's the C# code:

var OrdersByCustomer = db.Salesorders.GroupBy(so => so.CustomerId).
                                      Where(tos => tos.Count() > 5).

Finally, I need a Select method to create the object that the user wants. This example gives back a collection of anonymous objects that have a:

  • CustomerId property (pulled from the IGrouping collection's Key property)
  • TotaledValue property (derived by summing the IGrouping collection's members)
  • A list of the SalesOrders for that CustomerId (derived by doing a ToList on the IGrouping collection's members)

And, of course, I need to take only the first 10 objects.

Here's the code in C#:

var OrdersByCustomer = db.Salesorders.GroupBy(so => so.CustomerId).
                                      Where(tos => tos.Count() > 5).
                                      Select(tos => new {CustomerId = tos.Key, 
                                                         TotaledValue = tos.Sum(s => 
                                                         Orders = tos.ToList()}).Take(10);

And then in Visual Basic:

Dim OrdersByCustomer = db.SalesOrders.GroupBy(Function(so) so.CustomerId).
                                        Where(Function(tos) tos.Count() > 5).
                                        Select(Function(tos) New With {.CustomerId = tos.Key,
                                                                       .TotaledValue = 
                                                                       .Orders = 

Next time, we return to the regularly scheduled blog of random cool ways to do stuff without involving code.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at

comments powered by Disqus


  • What's New in Visual Studio 2019 v16.5 Preview 2

    The second preview of Visual Studio 2019 v16.5 has arrived with improvements across the flagship IDE, including the core experience and different development areas such as C++, Python, web, mobile and so on.

  • C# Shows Strong in Tech Skills Reports

    Microsoft's C# programming language continues to show strong in tech industry skills reports, with the most recent examples coming from a skills testing company and a training company.

  • Color Shards

    Sharing Data and Splitting Components in Blazor

    ASP.NET Core Version 3.1 has at least two major changes that you'll want to take advantage of. Well, Peter thinks you will. Depending on your background, your response to one of them may be a resounding “meh.”

  • Architecture Small Graphic

    Microsoft Ships Preview SDK, Guidance for New Dual-Screen Mobile Era

    Microsoft announced a new SDK and developer guidance for dealing with the new dual-screen mobile era, ushered in by the advent of ultra-portable devices such as the Surface Duo.

  • How to Create a Machine Learning Decision Tree Classifier Using C#

    After earlier explaining how to compute disorder and split data in his exploration of machine learning decision tree classifiers, resident data scientist Dr. James McCaffrey of Microsoft Research now shows how to use the splitting and disorder code to create a working decision tree classifier.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events