Click here to Skip to main content
15,881,027 members
Articles / Programming Languages / C#
Tip/Trick

Linq Joins with SelectMany

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
19 Jan 2011CPOL1 min read 32.6K   5   4
Linq Joins with SelectMany
SelectMany projects each element of a sequence to an IEnumerable<t></t> and flattens the resulting sequences into one sequence. In this post, I am going to show how you can use 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 is having relation with the order table.

Scenario
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:
C#
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:
C#
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 the 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:
C#
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 the SelectMany, the query is:
C#
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)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin 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:



Comments and Discussions

 
QuestionThank you Pin
Avi Farah2-Feb-19 5:49
Avi Farah2-Feb-19 5:49 
GeneralReason for my vote of 5 very nice Pin
dpflovely24-Jan-11 22:22
dpflovely24-Jan-11 22:22 
GeneralRe: thanks you can also vote my articles if you like it which is... Pin
Pranay Rana24-Jan-11 23:16
professionalPranay Rana24-Jan-11 23:16 
GeneralI edited the above for grammar/readability and redid the sum... Pin
Andrew Rissing18-Jan-11 5:09
Andrew Rissing18-Jan-11 5:09 

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

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