Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for an inner join in LINQ to SQL?

I'm writing a LINQ to SQL statement, and I'm after the standard syntax for a normal inner join with an ON clause in C#.

How do you represent the following in LINQ to SQL:

select DealerContact.* from Dealer  inner join DealerContact on Dealer.DealerID = DealerContact.DealerID 
like image 478
Glenn Slaven Avatar asked Sep 01 '08 01:09

Glenn Slaven


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.

What is the syntax of inner join?

Below is the basic syntax of Inner Join. Inner Join syntax basically compares rows of Table1 with Table2 to check if anything matches based on the condition provided in the ON clause. When the Join condition is met, it returns matched rows in both tables with the selected columns in the SELECT clause.

Which syntax is used in LINQ?

Most queries in the introductory Language Integrated Query (LINQ) documentation are written by using the LINQ declarative query syntax.


1 Answers

It goes something like:

from t1 in db.Table1 join t2 in db.Table2 on t1.field equals t2.field select new { t1.field2, t2.field3} 

It would be nice to have sensible names and fields for your tables for a better example. :)

Update

I think for your query this might be more appropriate:

var dealercontacts = from contact in DealerContact                      join dealer in Dealer on contact.DealerId equals dealer.ID                      select contact; 

Since you are looking for the contacts, not the dealers.

like image 57
Jon Limjap Avatar answered Sep 19 '22 03:09

Jon Limjap