Practical .NET

Getting All the Results in LINQ with Methods

Peter follows up on advice for joining collections with LINQ and getting all of the results, but this time, he uses the method-based syntax.

A little while back, I did a column on how to join two collections in LINQ to generate queries like, "Get me all of the customers, with their salesorders (if they have any)," or, "Get me all of the customers who have no salesorders." Generating these queries requires something like the SQL outer join and I walked through constructing that query in the column.

I got some feedback from readers of that column who use the LINQ method-based syntax (I used the SQL-like syntax in the column). This tip addresses that feedback by showing the method-based syntax, again in both Visual Basic and C# (though, I suspect that most programmers could translate from one syntax to the other).

Joining Collections with LINQ
Your first step is to call the GroupJoin method from the collection you want all the objects from (that's Customers in my example). You must pass the GroupJoin method four parameters:

  • The collection you want to join to (the collection that might be missing items; SalesOrders, in this case)
  • A lambda expression specifying the property from the collection you called GroupJoin from that you want to use in the join (the Id from the Customer object)
  • A lambda expression specifying the property from the collection in the first parameter that you want to use in the join (the CustomerId from the SalesOrder object)
  • The equivalent of a select statement that's used to build the object that you want returned

That final lambda expression is passed one object from the initial collection (Customers, in my case) and a collection of all the matching objects from the other collection (SalesOrders, in my case). In this example, I'm using that last lambda expression to build an anonymous object with two properties:

  • One called Key that holds the CustomerId
  • One called SalesOrders that holds all the SalesOrders

The resulting code looks like this in Visual Basic:

Dim = db.Customers.GroupJoin(db.SalesOrders,
                             Function(c) c.Id,
                             Function(s) s.CustomerId,
                             Function(c, s) New With {.Key = c, .SalesOrders = s})

In C#, it looks like this:

var mos = db.Customers.GroupJoin(db.Salesorders,
                                 c => c.Id,
                                 s => s.CustomerId,
                                 (c, s) => new { Key = c.Id, SalesOrders = s })

That will give you a result. Specifically, you'll end up with a collection of objects with each object having a property holding single CustomerId and another property holding the collection of SalesOrder objects for that CustomerId. That may not be the result you want.

Getting a Flattened Result
What you probably want is a collection of objects with each object having the property with the single Customer Id and another property holding a single SalesOrder object (the Customer Id will repeat across multiple objects, once for each SalesOrder). For this kind of transformation ("flattening" a result), you need to call the SelectMany method.

A warning: My SelectMany call is a little ugly, for two reasons. First, I need to call the DefaultIfAnyMethod on the SalesOrders collection -- this method will provide a null/Nothing value when there's no matching SalesOrder for a Customer (this prevents LINQ dropping Customers because there's no matching object). Second, I need to use an Immediate If to handle working with that null value when there's a Customer with no matching SalesOrder. In the following code, I've used the name ("mo") to refer to the objects returned from my GroupJoin method.

The SelectMany method requires two parameters (both lambda expressions). The first parameter specifies where matching objects are drawn from. In this case that's the property holding my SalesOrders collection. I call the DefaultIfEmpty method here to supply a value, even when the property is empty because there were no matching salesorders for the Customer.

In C#, the code looks like this:

var res = mos.SelectMany(mo => mo.SalesOrders.DefaultIfEmpty(),

In Visual Basic, like this:

Dim res = mos.SelectMany(Function(mo) mo.SalesOrders.DefaultIfEmpty(),

The second parameter is like a Select method: It's where you specify the object you'll return from the SelectMany call. The lambda expression you pass here must accept two parameters: each object from the collection you're processing (in this case, that's the object from my GroupJoin) and the matching object from the first parameter.

I return an anonymous object consisting of the Customer Id and the OrderId from the SalesOrder selected in the first parameter. To handle the null/Nothing values I get from that first parameter, I use an Immediate If to supply a default value.

Putting that all together, my SelectMany looks like this in C#:

Dim res = mos.SelectMany(mo => mo.SalesOrders.DefaultIfEmpty(),
                        (c, s) => new { c.Key.Id, OrderId = s == null ? "Not Found" : s.Id.ToString() });

and this in Visual Basic:

Dim res = mos.SelectMany(Function(mo) mo.SalesOrders.DefaultIfEmpty(),
                         Function(c, s) New With {c.Key.Id, .OrderId = If(s Is Nothing, "Not Found", s.Id.ToString)})

If you want to do it in one LINQ statement (in order, I suspect, to frighten the next programmer), it looks like this in C#:

var res = db.Customers.GroupJoin(db.Salesorders,
                                 c => c.Id,
                                 s => s.CustomerId,
                                (c, s) => new { Key = c, SalesOrders = s }).
                       SelectMany(mo => mo.SalesOrders.DefaultIfEmpty(),
                                 (c, s) => new { c.Key.Id, OrderId = s == null ? "Not Found" : s.Id.ToString() });

and this in Visual Basic:

Dim = db.Customers.GroupJoin(db.SalesOrders,
                             Function(c) c.Id,
                             Function(s) s.CustomerId,
                             Function(c, s) New With {.Key = c, .SalesOrders = s}).
                    SelectMany(Function(mo) mo.SalesOrders.DefaultIfEmpty(),
                               Function(c, s) New With {c.Key.Id, .OrderId = If(s Is Nothing, "Not Found", s.Id.ToString)})

(Degree of fright is dependent on the next programmer's level of expertise, of course ...)

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