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

<p>How do you represent the following in LINQ to SQL:</p>

<pre><code>select DealerContact.*
from Dealer 
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
</code></pre>

[{'answer_id': 37332, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 10, 'selected': True, 'text': '<p>It goes something like:</p>\n\n<pre><code>from t1 in db.Table1\njoin t2 in db.Table2 on t1.field equals t2.field\nselect new { t1.field2, t2.field3}\n</code></pre>\n\n<p>It would be nice to have sensible names and fields for your tables for a better example. :)</p>\n\n<p><strong>Update</strong></p>\n\n<p>I think for your query this might be more appropriate:</p>\n\n<pre><code>var dealercontacts = from contact in DealerContact\n                     join dealer in Dealer on contact.DealerId equals dealer.ID\n                     select contact;\n</code></pre>\n\n<p>Since you are looking for the contacts, not the dealers.</p>\n'}, {'answer_id': 37333, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 5, 'selected': False, 'text': '<p>Use <a href="http://www.hookedonlinq.com/JoinOperator.ashx" rel="noreferrer">Linq Join</a> operator:</p>\n\n<pre><code>var q =  from d in Dealer\n         join dc in DealerConact on d.DealerID equals dc.DealerID\n         select dc;\n</code></pre>\n'}, {'answer_id': 1824432, 'author': 'herste', 'author_id': 221890, 'author_profile': 'https://Stackoverflow.com/users/221890', 'pm_score': 6, 'selected': False, 'text': '<pre><code>var results = from c in db.Companies\n              join cn in db.Countries on c.CountryID equals cn.ID\n              join ct in db.Cities on c.CityID equals ct.ID\n              join sect in db.Sectors on c.SectorID equals sect.ID\n              where (c.CountryID == cn.ID) &amp;&amp; (c.CityID == ct.ID) &amp;&amp; (c.SectorID == company.SectorID) &amp;&amp; (company.SectorID == sect.ID)\n              select new { country = cn.Name, city = ct.Name, c.ID, c.Name, c.Address1, c.Address2, c.Address3, c.CountryID, c.CityID, c.Region, c.PostCode, c.Telephone, c.Website, c.SectorID, Status = (ContactStatus)c.StatusID, sector = sect.Name };\n\n\nreturn results.ToList();\n</code></pre>\n'}, {'answer_id': 3851487, 'author': 'CleverPatrick', 'author_id': 22399, 'author_profile': 'https://Stackoverflow.com/users/22399', 'pm_score': 8, 'selected': False, 'text': '<p>And because I prefer the expression chain syntax, here is how you do it with that:</p>\n\n<pre><code>var dealerContracts = DealerContact.Join(Dealer, \n                                 contact =&gt; contact.DealerId,\n                                 dealer =&gt; dealer.DealerId,\n                                 (contact, dealer) =&gt; contact);\n</code></pre>\n'}, {'answer_id': 5170765, 'author': 'the_joric', 'author_id': 229949, 'author_profile': 'https://Stackoverflow.com/users/229949', 'pm_score': 5, 'selected': False, 'text': '<p>basically LINQ <strong>join</strong> operator provides no benefit for SQL. I.e. the following query</p>\n\n<pre><code>var r = from dealer in db.Dealers\n   from contact in db.DealerContact\n   where dealer.DealerID == contact.DealerID\n   select dealerContact;\n</code></pre>\n\n<p>will result in INNER JOIN in SQL</p>\n\n<p><strong>join</strong> is useful for IEnumerable&lt;> because it is more efficient: </p>\n\n<pre><code>from contact in db.DealerContact  \n</code></pre>\n\n<p>clause would be re-executed for every <strong>dealer</strong>\nBut for IQueryable&lt;> it is not the case. Also <strong>join</strong> is less flexible.</p>\n'}, {'answer_id': 8251984, 'author': 'Kirk Broadhurst', 'author_id': 146077, 'author_profile': 'https://Stackoverflow.com/users/146077', 'pm_score': 5, 'selected': False, 'text': "<p>You create a foreign key, and LINQ-to-SQL creates navigation properties for you. Each <code>Dealer</code> will then have a collection of <code>DealerContacts</code> which you can select, filter, and manipulate.</p>\n\n<pre><code>from contact in dealer.DealerContacts select contact\n</code></pre>\n\n<p>or</p>\n\n<pre><code>context.Dealers.Select(d =&gt; d.DealerContacts)\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 11334923, 'author': 'Gert Arnold', 'author_id': 861716, 'author_profile': 'https://Stackoverflow.com/users/861716', 'pm_score': 4, 'selected': False, 'text': '<p>Actually, often it is better not to join, in linq that is. When there are navigation properties a very succinct way to write your linq statement is:</p>\n\n<pre class="lang-cs prettyprint-override"><code>from dealer in db.Dealers\nfrom contact in dealer.DealerContacts\nselect new { whatever you need from dealer or contact }\n</code></pre>\n\n<p>It translates to a where clause:</p>\n\n<pre class="lang-sql prettyprint-override"><code>SELECT &lt;columns&gt;\nFROM Dealer, DealerContact\nWHERE Dealer.DealerID = DealerContact.DealerID\n</code></pre>\n'}, {'answer_id': 13096990, 'author': 'Sandeep Shekhawat', 'author_id': 1390850, 'author_profile': 'https://Stackoverflow.com/users/1390850', 'pm_score': 2, 'selected': False, 'text': '<pre><code>OperationDataContext odDataContext = new OperationDataContext();    \n        var studentInfo = from student in odDataContext.STUDENTs\n                          join course in odDataContext.COURSEs\n                          on student.course_id equals course.course_id\n                          select new { student.student_name, student.student_city, course.course_name, course.course_desc };\n</code></pre>\n\n<p>Where student and course tables have primary key and foreign key relationship</p>\n'}, {'answer_id': 16356390, 'author': 'Prasad KM', 'author_id': 2346395, 'author_profile': 'https://Stackoverflow.com/users/2346395', 'pm_score': -1, 'selected': False, 'text': '<p>One Best example</p>\n\n<p>Table Names :  <code>TBL_Emp</code> and <code>TBL_Dep</code></p>\n\n<pre><code>var result = from emp in TBL_Emp join dep in TBL_Dep on emp.id=dep.id\nselect new\n{\n emp.Name;\n emp.Address\n dep.Department_Name\n}\n\n\nforeach(char item in result)\n { // to do}\n</code></pre>\n'}, {'answer_id': 23510671, 'author': 'Uthaiah', 'author_id': 1488323, 'author_profile': 'https://Stackoverflow.com/users/1488323', 'pm_score': 2, 'selected': False, 'text': '<p>Use <a href="http://msdn.microsoft.com/en-us/library/bb397941.aspx" rel="nofollow">LINQ joins</a> to perform Inner Join.</p>\n\n<pre><code>var employeeInfo = from emp in db.Employees\n                   join dept in db.Departments\n                   on emp.Eid equals dept.Eid \n                   select new\n                   {\n                    emp.Ename,\n                    dept.Dname,\n                    emp.Elocation\n                   };\n</code></pre>\n'}, {'answer_id': 26339802, 'author': 'Milan', 'author_id': 3804209, 'author_profile': 'https://Stackoverflow.com/users/3804209', 'pm_score': 2, 'selected': False, 'text': '<p>try instead this,</p>\n\n<pre><code>var dealer = from d in Dealer\n             join dc in DealerContact on d.DealerID equals dc.DealerID\n             select d;\n</code></pre>\n'}, {'answer_id': 28985025, 'author': 'Ajay', 'author_id': 6998210, 'author_profile': 'https://Stackoverflow.com/users/6998210', 'pm_score': 2, 'selected': False, 'text': '<p>Try this :</p>\n\n<pre><code>     var data =(from t1 in dataContext.Table1 join \n                 t2 in dataContext.Table2 on \n                 t1.field equals t2.field \n                 orderby t1.Id select t1).ToList(); \n</code></pre>\n'}, {'answer_id': 29310640, 'author': 'Jon Schneider', 'author_id': 12484, 'author_profile': 'https://Stackoverflow.com/users/12484', 'pm_score': 6, 'selected': False, 'text': '<p>To extend the expression chain syntax <a href="https://stackoverflow.com/a/3851487/12484">answer</a> by Clever Human:</p>\n\n<p>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:</p>\n\n<pre><code>var dealerInfo = DealerContact.Join(Dealer, \n                              dc =&gt; dc.DealerId,\n                              d =&gt; d.DealerId,\n                              (dc, d) =&gt; new { DealerContact = dc, Dealer = d })\n                          .Where(dc_d =&gt; dc_d.Dealer.FirstName == "Glenn" \n                              &amp;&amp; dc_d.DealerContact.City == "Chicago")\n                          .Select(dc_d =&gt; new {\n                              dc_d.Dealer.DealerID,\n                              dc_d.Dealer.FirstName,\n                              dc_d.Dealer.LastName,\n                              dc_d.DealerContact.City,\n                              dc_d.DealerContact.State });\n</code></pre>\n\n<p>The interesting part is the lambda expression in line 4 of that example: </p>\n\n<pre><code>(dc, d) =&gt; new { DealerContact = dc, Dealer = d }\n</code></pre>\n\n<p>...where we construct a new anonymous-type object which has as properties the DealerContact and Dealer records, along with all of their fields.  </p>\n\n<p>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 <code>dc_d</code> as a name for the anonymous object we built which has both the DealerContact and Dealer records as its properties.</p>\n'}, {'answer_id': 43161807, 'author': 'Md Shahriar', 'author_id': 4211947, 'author_profile': 'https://Stackoverflow.com/users/4211947', 'pm_score': 2, 'selected': False, 'text': '<p>Inner join two tables in linq C#</p>\n\n<pre><code>var result = from q1 in table1\n             join q2 in table2\n             on q1.Customer_Id equals q2.Customer_Id\n             select new { q1.Name, q1.Mobile, q2.Purchase, q2.Dates }\n</code></pre>\n'}, {'answer_id': 43177753, 'author': 'Ankita_systematix', 'author_id': 7348760, 'author_profile': 'https://Stackoverflow.com/users/7348760', 'pm_score': 1, 'selected': False, 'text': '<pre><code>var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID\nselect new{\ndealer.Id,\ndealercontact.ContactName\n\n}).ToList();\n</code></pre>\n'}, {'answer_id': 47390465, 'author': 'sanket parikh', 'author_id': 5414397, 'author_profile': 'https://Stackoverflow.com/users/5414397', 'pm_score': 1, 'selected': False, 'text': '<pre><code>var data=(from t in db.your tableName(t1) \n          join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname\n          (where condtion)).tolist();\n</code></pre>\n'}, {'answer_id': 47950778, 'author': 'Sarfraj Sutar', 'author_id': 8676193, 'author_profile': 'https://Stackoverflow.com/users/8676193', 'pm_score': 1, 'selected': False, 'text': '<pre><code>var list = (from u in db.Users join c in db.Customers on u.CustomerId equals c.CustomerId where u.Username == username\n   select new {u.UserId, u.CustomerId, u.ClientId, u.RoleId, u.Username, u.Email, u.Password, u.Salt, u.Hint1, u.Hint2, u.Hint3, u.Locked, u.Active,c.ProfilePic}).First();\n</code></pre>\n\n<p>Write table names you want, and initialize the select to get the result of fields.</p>\n'}, {'answer_id': 48500439, 'author': 'ammad khan', 'author_id': 9281814, 'author_profile': 'https://Stackoverflow.com/users/9281814', 'pm_score': 2, 'selected': False, 'text': '<pre><code>var q=(from pd in dataContext.tblProducts join od in dataContext.tblOrders on pd.ProductID equals od.ProductID orderby od.OrderID select new { od.OrderID,\n pd.ProductID,\n pd.Name,\n pd.UnitPrice,\n od.Quantity,\n od.Price,\n }).ToList(); \n</code></pre>\n'}, {'answer_id': 57424051, 'author': 'Rutu', 'author_id': 11905637, 'author_profile': 'https://Stackoverflow.com/users/11905637', 'pm_score': 1, 'selected': False, 'text': '<p>from d1 in DealerContrac join d2 in DealerContrac on d1.dealearid equals d2.dealerid select new {dealercontract.*}</p>\n'}]
['https://Stackoverflow.com/questions/37324', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2975/']