Simple SelectMany Example with Associated Collection in C# LINQ

The SelectMany operator implements cross and outer joins, and flattens sequences into elements that
resemble rows of relational views based on joins.SelectMany returns from the source object a many – to –
one sequence having the set of properties specified by the selector that ’ s passed to the lambda function.

var query6 = CustomerList.Where(c => c.Country == "USA")
                                     .SelectMany(c => c.Orders);
 
query6 = from c in CustomerList
         where c.Country == "USA"
         from o in c.Orders
         select o;
 
foreach (var o in query6)
    sbResult.Append(String.Format("CustomerID = {0}, CompanyName = {1}, OrderID = {2}\r\n",
                               o.CustomerID, o.Customer.CompanyName, o.OrderID));

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.