Click here to Skip to main content
15,889,808 members
Articles / All Topics

Linq Join on Mutiple columns using Anonymous type

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
2 Oct 2011CPOL 37.8K   1   1
Linq Join on Mutiple columns using Anonymous type

I was working on the project using LINQ. I got the requirement to join the two entities in multiple columns.

For example, consider the following image. There are two entities, Distributor and Customer related to each other.

Now I want to find out all customers who live in same city where the distributor lives.

So to find that out, I have to make a join between Customer and Distributor. And to achieve this, I need to join by using multiple columns City, State, Country, ID. (Note: I am using id in join because later on I want to get which distributor is near which customer).

Now with the LINQ, you can join two entities on multiple columns by creating one anonymous type.

C#
EmployeeDataContext edb= new EmployeeDataContext();
var cust = from c in edb.Customers
           join d in edb.Distributors on
             new { CityID = c.CityId, StateID = c.StateId, CountryID = c.CountryId, 
                   Id = c.DistributorId }    
           equals
             new { CityID = d.CityId, StateID = d.StateId, CountryID = d.CountryId, 
                   Id = d.DistributorId }    
           select c;

Note: As anonymous types are used to join entities on multiple columns, make sure that both are equal and they must have the same properties in the same order. Otherwise it doesn't get complied and you get an error.

Once you are done, run the code and you will see the following query in your SQL profiler or you can also use the Visual Studio feature to get the query.

SQL
SELECT [t0].[Id], [t0].[Name], [t0].[EmailId], [t0].[CityId], [t0].[StateId], 
[t0].[CountryId], [t0].[PinCode], [t0].[DistributorId]
FROM [dbo].[Customer] AS [t0]
INNER JOIN 
[dbo].[Distributor] AS [t1] ON 
([t0].[CityId] = [t1].[CityId]) 
    AND ([t0].[StateId] = [t1].[StateId]) 
    AND  ([t0].[CountryId] = [t1].[CountryId]) 
    AND ([t0].[DistributorId] =[t1].[DistributorId])

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

 
Question[My vote of 1] Question Pin
Suraj854-Jun-12 3:07
Suraj854-Jun-12 3:07 

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.