Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert multiple inner joins in SQL to LINQ?

I've got the basics of LINQ-to-SQL down, but I've been struggling trying to get JOINs to work properly. I'd like to know how to convert the following to LINQ-to-SQL (ideally using method chaining, as that is my preferred format).

SELECT      c.CompanyId, c.CompanyName,             p.FirstName + ' ' + p.LastName as AccountCoordinator,             p2.FirstName + ' ' + p2.LastName as AccountManager FROM        dbo.Companies c INNER JOIN  dbo.Persons p ON          c.AccountCoordinatorPersonId = p.PersonId INNER JOIN  dbo.Persons p2 ON          c.AccountManagerPersonId = p2.PersonId 
like image 995
Ed Sinek Avatar asked Nov 06 '10 21:11

Ed Sinek


People also ask

How use inner join in LINQ query?

A simple inner join that correlates elements from two data sources based on a simple key. An inner join that correlates elements from two data sources based on a composite key. A composite key, which is a key that consists of more than one value, enables you to correlate elements based on more than one property.

Can we do joins in LINQ?

In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.

Is LINQ join inner or outer?

When you use the LINQ join clause in the query expression syntax to combine two sets of related information, you perform an inner join. This means that you provide an expression that determines for each item in the first sequence, the matching items in the second.

Can I join a table to a list using LINQ?

When the query actually runs, LINQ should be able to create a temp table or table variable with the data from the local list and then join on that. This is a feature that should absolutely be included in the framework.


1 Answers

Using query syntax:

from c in dbo.Companies join p in dbo.Persons on c.AccountCoordinatorPersonId equals p.PersonId join p2 in dbo.Persons on c.AccountManagerPersonId equals p2.PersonId select new {     c.CompanyId,     c.CompanyName,     AccountCoordinator = p.FirstName + ' ' + p.Surname,     AccountManager = p2.FirstName + ' ' + p2.Surname } 

Using method chaining:

dbo.Companies.Join(dbo.Persons,                     c => c.AccountCoordinatorPersonId,                      p => p.PersonId,                      (c, p) => new                     {                          Company = c,                          AccountCoordinator = p.FirstName + ' ' + p.Surname                      })              .Join(dbo.Persons,                      c => c.Company.AccountManagerPersonId,                      p2 => p2.PersonId,                      (c, p2) => new                     {                          c.Company.CompanyId,                          c.Company.CompanyName,                          c.AccountCoordinator,                          AccountManager = p2.FirstName + ' ' + p2.Surname                     }); 
like image 72
Dave D Avatar answered Oct 22 '22 04:10

Dave D