Click here to Skip to main content
11,567,143 members (48,349 online)
Click here to Skip to main content

LINQ Joins with SelectMany

, 26 Jul 2011 CPOL 20.4K 2
Rate this:
Please Sign up or sign in to vote.
SelectMany is a way to flatten several results into a single sequence.

SelectMany projects takes each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence. In this post I am going to show how you can use the SelectMany extension method to achieve the join between related tables easily without writing long queries.

To understand this, consider the example below:

As shown in the above image, customer has a relation with the order table.

Scenario 1

Find all of the users who have an order.

To achieve the above requirement, you need to apply an inner join between the tables. The LINQ query to do this is:

var CustomerWithOrders = from c in Customers
  from o in c.Orders
    select new { 
           c.Name, 
           p.Description, 
           o.Price 
          }

The query above inner joins the customer table with the order table and returns those customers having orders.

The shorter way to achieve the above is to use SelectMany:

Customers.SelectMany (
      c => c.Orders, 
      (c, o) => 
         new  
         {
            Name = c.Name, 
            Description = o.Description, 
            Price = o.Price
         }
   )

Scenario 2

Find all of the users who have an order and display N/A for users who do not.

To achieve the above requirement, you need to apply an outer join between the tables. The LINQ query to do this is:

var CustomerWithOrders = from c in Customers
 from o in c.Orders.DefaultIfEmpty()
 select new { 
         Name =c.Name,
         Description = ( o.Description ?? "N/A"), 
         Price = (((decimal?) o.Price) ?? 0)  
        }

The query above outer joins the customer table with the order table and returns all of the customers with or without orders.

But with SelectMany, the query is:

Customers.SelectMany (
      c => c.Orders.DefaultIfEmpty (), 
      (c, o) => 
         new  
         {
            Name = c.Name, 
            Description = (o.Description ?? "N/A"), 
            Price = ((Decimal?)(o.Price) ?? 0)
         }
   )

Summary

SelectMany is a way to flatten several results into a single sequence.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)


Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



You may also be interested in...

Comments and Discussions

 
QuestionDataSet or DataTable? Pin
Michael Gaida2-Jul-14 2:11
memberMichael Gaida2-Jul-14 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 26 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid