C# LINQ to SQL 中内部联接的语法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:44:45  来源:igfitidea点击:

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

提问by Glenn Slaven

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

我正在编写一个 LINQ to SQL 语句,我正在使用ONC# 中带有子句的普通内部连接的标准语法。

How do you represent the following in LINQ to SQL:

您如何在 LINQ to SQL 中表示以下内容:

select DealerContact.*
from Dealer 
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID

采纳答案by Jon Limjap

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.

由于您正在寻找联系人,而不是经销商。

回答by aku

Use Linq Joinoperator:

使用Linq 连接运算符:

var q =  from d in Dealer
         join dc in DealerConact on d.DealerID equals dc.DealerID
         select dc;

回答by herste

var results = from c in db.Companies
              join cn in db.Countries on c.CountryID equals cn.ID
              join ct in db.Cities on c.CityID equals ct.ID
              join sect in db.Sectors on c.SectorID equals sect.ID
              where (c.CountryID == cn.ID) && (c.CityID == ct.ID) && (c.SectorID == company.SectorID) && (company.SectorID == sect.ID)
              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 };


return results.ToList();

回答by CleverPatrick

And because I prefer the expression chain syntax, here is how you do it with that:

因为我更喜欢表达式链语法,这里是你如何使用它:

var dealerContracts = DealerContact.Join(Dealer, 
                                 contact => contact.DealerId,
                                 dealer => dealer.DealerId,
                                 (contact, dealer) => contact);

回答by the_joric

basically LINQ joinoperator provides no benefit for SQL. I.e. the following query

基本上 LINQ连接运算符对 SQL 没有任何好处。即以下查询

var r = from dealer in db.Dealers
   from contact in db.DealerContact
   where dealer.DealerID == contact.DealerID
   select dealerContact;

will result in INNER JOIN in SQL

将导致 SQL 中的 INNER JOIN

joinis useful for IEnumerable<> because it is more efficient:

join对 IEnumerable<> 很有用,因为它更高效:

from contact in db.DealerContact  

clause would be re-executed for every dealerBut for IQueryable<> it is not the case. Also joinis less flexible.

条款将针对每个经销商重新执行 但对于 IQueryable<> 情况并非如此。还加入不够灵活。

回答by Kirk Broadhurst

You create a foreign key, and LINQ-to-SQL creates navigation properties for you. Each Dealerwill then have a collection of DealerContactswhich you can select, filter, and manipulate.

您创建外键,LINQ-to-SQL 会为您创建导航属性。Dealer然后每个都有一个集合DealerContacts,您可以选择、过滤和操作这些集合。

from contact in dealer.DealerContacts select contact

or

或者

context.Dealers.Select(d => d.DealerContacts)

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.

如果您不使用导航属性,您就会错过 LINQ-to-SQL 的主要优势之一——映射对象图的部分。

回答by Gert Arnold

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:

实际上,通常最好不要加入,即在 linq 中。当有导航属性时,编写 linq 语句的一种非常简洁的方法是:

from dealer in db.Dealers
from contact in dealer.DealerContacts
select new { whatever you need from dealer or contact }

It translates to a where clause:

它转换为 where 子句:

SELECT <columns>
FROM Dealer, DealerContact
WHERE Dealer.DealerID = DealerContact.DealerID

回答by Sandeep Shekhawat

OperationDataContext odDataContext = new OperationDataContext();    
        var studentInfo = from student in odDataContext.STUDENTs
                          join course in odDataContext.COURSEs
                          on student.course_id equals course.course_id
                          select new { student.student_name, student.student_city, course.course_name, course.course_desc };

Where student and course tables have primary key and foreign key relationship

学生和课程表具有主键和外键关系的地方

回答by Prasad KM

One Best example

一个最好的例子

Table Names : TBL_Empand TBL_Dep

表名: TBL_EmpTBL_Dep

var result = from emp in TBL_Emp join dep in TBL_Dep on emp.id=dep.id
select new
{
 emp.Name;
 emp.Address
 dep.Department_Name
}


foreach(char item in result)
 { // to do}

回答by Uthaiah

Use LINQ joinsto perform Inner Join.

使用LINQ 联接执行内部联接。

var employeeInfo = from emp in db.Employees
                   join dept in db.Departments
                   on emp.Eid equals dept.Eid 
                   select new
                   {
                    emp.Ename,
                    dept.Dname,
                    emp.Elocation
                   };