Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Linq Join on Mutiple columns using Anonymous type

, 2 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

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)

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:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
Question[My vote of 1] Question PinmemberCorruptmind4-Jun-12 4:07 

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.141223.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid