我正在编写一个LINQ to SQL语句,我正在使用ON
C#中的子句进行普通内连接的标准语法.
如何在LINQ to SQL中表示以下内容:
select DealerContact.* from Dealer inner join DealerContact on Dealer.DealerID = DealerContact.DealerID
Jon Limjap.. 558
它类似于:
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;
既然您正在寻找联系人,而不是经销商.
它类似于:
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;
既然您正在寻找联系人,而不是经销商.
因为我更喜欢表达式链接语法,所以这是你如何做到这一点:
var dealerContracts = DealerContact.Join(Dealer, contact => contact.DealerId, dealer => dealer.DealerId, (contact, dealer) => contact);
要扩展Clever Human 的表达链语法答案:
如果你想在两个表连接在一起的字段上做某些事情(比如过滤或选择) - 而不只是这两个表中的一个 - 你可以在Join方法的最后一个参数的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 }
...我们构造一个新的匿名类型对象,它具有DealerContact和Dealer记录的属性以及它们的所有字段.
然后,我们可以在筛选和选择结果时使用这些记录中的字段,如示例的其余部分所示,该示例使用dc_d
我们构建的匿名对象的名称,该对象同时具有DealerContact和Dealer记录作为其属性.
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();
使用Linq Join运算符:
var q = from d in Dealer join dc in DealerConact on d.DealerID equals dc.DealerID select dc;
您创建了一个外键,LINQ-to-SQL为您创建了导航属性.Dealer
然后每个都有一个集合DealerContacts
,您可以选择,过滤和操作.
from contact in dealer.DealerContacts select contact
要么
context.Dealers.Select(d => d.DealerContacts)
如果您没有使用导航属性,那么您将错过LINQ-to-SQL的主要优势之一 - 映射对象图的部分.
基本上LINQ join运算符对SQL没有任何好处.即以下查询
var r = from dealer in db.Dealers from contact in db.DealerContact where dealer.DealerID == contact.DealerID select dealerContact;
将导致SQL中的INNER JOIN
join对IEnumerable <>非常有用,因为它更有效:
from contact in db.DealerContact
每个经销商都会重新执行该条款 但对于IQueryable <>则不是这样.还加入不够灵活.
实际上,通常最好不要加入linq.当有导航属性时,编写linq语句的一种非常简洁的方法是:
from dealer in db.Dealers
from contact in dealer.DealerContacts
select new { whatever you need from dealer or contact }
它转换为where子句:
SELECT
FROM Dealer, DealerContact
WHERE Dealer.DealerID = DealerContact.DealerID