我正在编写LINQ to SQL语句,并且我遵循C#中ON
子句的正常内连接的标准语法。LINQ to SQL中的内部连接的语法是什么?
你怎么代表以下的LINQ到SQL:
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
我正在编写LINQ to SQL语句,并且我遵循C#中ON
子句的正常内连接的标准语法。LINQ to SQL中的内部连接的语法是什么?
你怎么代表以下的LINQ到SQL:
select DealerContact.*
from Dealer
inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
它去是这样的:
from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}
这将是很好的为您的表显名和字段美好例。 :)
更新
我想为您的查询,这可能是比较合适的:
var dealercontacts = from contact in DealerContact
join dealer in Dealer on contact.DealerId equals dealer.ID
select contact;
既然你正在寻找的接触,而不是经销商。
谢谢,从现在开始,我将使用**明智的名称**作为最佳实践,这在linq中是有意义的,而不是从`c或从t1`开始的 – stom 2016-01-14 07:26:42
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();
嗨, 请问你能告诉我这部分是关于什么? 状态=(ContactStatus)c.StatusID 我在片段感兴趣expecially:(ContactStatus)c.StatusID 问候 马里乌什 – Mariusz 2010-03-12 23:55:11
@aristo - 看代码,我猜'ContactStatus`是真的是一个枚举,`c.StatusID`不是一个真正的ID,而是枚举的数值。如果我是对的,`(ContactStatus)c.StatusID`实际上只是将一个整数转换为一个枚举。 – 2010-07-14 22:13:43
而且因为我更喜欢表达链语法,这里是你如何与做:
var dealerContracts = DealerContact.Join(Dealer,
contact => contact.DealerId,
dealer => dealer.DealerId,
(contact, dealer) => contact);
如果您需要过滤或选择来自*两个*联合表格的字段,而不是仅在两个表格之一的字段(此答案的示例中的DealerContact表格)中,则可以使用以下示例:http://stackoverflow.com/ a/29310640/12484 – 2015-03-27 21:41:44
基本上LINQ 加入运营商提供对SQL没有好处。即下面的查询
var r = from dealer in db.Dealers
from contact in db.DealerContact
where dealer.DealerID == contact.DealerID
select dealerContact;
将导致INNER JOIN在SQL
加入,因为它是更有效是IEnumerable的<>有用:
from contact in db.DealerContact
条款将被用于重新执行每经销商 但对于IQueryable <>情况并非如此。另外加入不那么灵活。
您创建了一个外键,LINQ-to-SQL为您创建导航属性。然后每个Dealer
将有一个DealerContacts
的集合,您可以选择,过滤和操作。
from contact in dealer.DealerContacts select contact
或
context.Dealers.Select(d => d.DealerContacts)
如果你不使用导航属性,你就错过了对LINQ到SQL的主要优点之一 - 对象图映射的一部分。
哦,男人,你节省了我的时间,我不需要再处理这些愚蠢的连接了! – Tomas 2013-01-31 15:16:30
其实,往往最好不要加入,在linq中就是这样。当有导航属性来编写你的LINQ语句非常简洁的方式是:
from dealer in db.Dealers
from contact in dealer.DealerContacts
select new { whatever you need from dealer or contact }
它转化为一个where子句:
SELECT <columns>
FROM Dealer, DealerContact
WHERE Dealer.DealerID = DealerContact.DealerID
带有多个“from”子句的LINQ查询(如本例中)在表达式链式语法中看起来如何?可能吗? – 2015-05-27 20:07:11
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 };
如果学生和课程表通过主键和外关键关系
一个最佳示例
表名称:TBL_Emp
和TBL_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}
使用LINQ joins进行内连接。
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
};
尝试,而不是这个,
var dealer = from d in Dealer
join dc in DealerContact on d.DealerID equals dc.DealerID
select d;
试试这个:
var data =(from t1 in dataContext.Table1 join
t2 in dataContext.Table2 on
t1.field equals t2.field
orderby t1.Id select t1).ToList();
要由聪明人延长表达链语法answer:
如果你想要做的事(像过滤器或选择)来自两个表格的字段被连接在一起 - 而不是仅仅其中之一两个表 - 你可以在最后一个参数的lambda表达式创建新对象的加入方法结合这两个表,例如:
var dealerInfo = DealerContact.Join(Dealer,
dc => dc.DealerId,
d => d.DealerId,
(dc, d) => new { DealerContact = dc, Dealer = d })
.Where(dc_d => dc_d.Dealer.FirstName == "Glenn"
&& dc_d.DealerContact.City == "Chicago")
.Select(dc_d => new {
dc_d.Dealer.DealerID,
dc_d.Dealer.FirstName,
dc_d.Dealer.LastName,
dc_d.DealerContact.City,
dc_d.DealerContact.State });
有趣的部分是在这4行lambda表达式例如:
(dc, d) => new { DealerContact = dc, Dealer = d }
...我们构建具有作为属性的代理商联系和经销商的记录,他们所有的字段沿着新的匿名类型的对象。
然后,我们可以使用这些记录中的字段来筛选和选择结果,如示例的其余部分所示,该示例使用dc_d
作为我们构建的匿名对象的名称,该对象同时具有DealerContact和Dealer记录其属性。
内加入Linq中的C#两个表
VAR的结果=
从Q1在表1
表2中加入Q2
上q1.Customer_Id等于q2.Customer_Id
选择新{q1.Name,Q1。移动,q2.Purchase,q2.Dates}
var Data= (from dealer in Dealer join dealercontact in DealerContact on dealer.ID equals dealercontact.DealerID
select new{
dealer.Id,
dealercontact.ContactName
}).ToList();
var data=(from t in db.your tableName(t1)
join s in db.yourothertablename(t2) on t1.fieldname equals t2.feldname
(where condtion)).tolist();
var list = (from u in db.Users join c in db.Customers on u.CustomerId equals c.CustomerId where u.Username == username
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();
写出您想要的表名,并初始化select以获取字段的结果。
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,
pd.ProductID,
pd.Name,
pd.UnitPrice,
od.Quantity,
od.Price,
}).ToList();
您可能会发现[LINQ中的SQL查询](http://www.codeducky.org/sql-queries-in-linq/#inner-join)有帮助。它解释了如何将常见的SQL查询(包括内部连接)转换为LINQ语法。 – 2014-05-13 22:18:51
如果你在表格之间有一个外键,你应该看看下面的Kirk Broadhurst的答案。 – Guilherme 2016-11-20 22:35:21
你应该复用你的表名。关于许多经销商的表格(有关条目)应称为*经销商*,而不是经销商。 – ANeves 2017-08-21 10:32:25