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 => 
                                                           s.TotalValue),
                                                         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 = 
                                                                         tos.Sum(
                                                                         Function(s) 
                                                                         s.TotalValue),
                                                                       .Orders = 
                                                                         tos.ToList()}).Take(10)

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 http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube