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
Use Linq Join operator:
To extend the expression chain syntax answer by Clever Human:
If you wanted to do things (like filter or select) on fields from both tables being joined together -- instead on just one of those two tables -- you could create a new object in the lambda expression of the final parameter to the Join method incorporating both of those tables, for example:
The interesting part is the lambda expression in line 4 of that example:
...where we construct a new anonymous-type object which has as properties the DealerContact and Dealer records, along with all of their fields.
We can then use fields from those records as we filter and select the results, as demonstrated by the remainder of the example, which uses
dc_d
as a name for the anonymous object we built which has both the DealerContact and Dealer records as its properties.Inner join two tables in linq C#
Use LINQ joins to perform Inner Join.
Try this :
You create a foreign key, and LINQ-to-SQL creates navigation properties for you. Each
Dealer
will then have a collection ofDealerContacts
which you can select, filter, and manipulate.or
If you're not using navigation properties, you're missing out one of the main benefits on LINQ-to-SQL - the part that maps the object graph.