如何通过Linq从同一个表中连接多个列?
例如:我已经有了......
join c in db.table2 on table2.ID equals table1.ID
我需要添加这个......
join d in db.table2 on table2.Country equals table1.Country
Pete Haas.. 31
这是我能够让它工作的唯一方法(在c#中).
var qry = from t1 in table1 join t2 in table2 on new {t1.ID,t1.Country} equals new {t2.ID,t2.Country} ...
Rik Hemsley.. 14
您可以将查询放在Where子句中,而不是使用join运算符.
join操作符支持VB.NET中的多个子句,但不支持C#.
或者,您可以使用ANSI-82样式的"SQL"语法,例如:
from t1 in table1 from t2 in table1 where t1.x == t2.x && t1.y == t2.y
Greg Ogle.. 13
来自http://www.onedotnetway.com/linq-to-sql-join-on-multiple-conditions/
这两个表都有PostCode和CouncilCode作为公共字段.假设我们想要从ShoppingMall中检索所有记录,其中PostCode和HouseCode on House匹配.这要求我们使用两列进行连接.在LINQ中,可以使用匿名类型完成连接.这是一个例子.
var query = from s in context.ShoppingMalls join h in context.Houses on new { s.CouncilCode, s.PostCode } equals new { h.CouncilCode, h.PostCode } select s;
小智.. 6
var query = from s in context.ShoppingMalls join h in context.Houses on new {CouncilCode=s.CouncilCode, PostCode=s.PostCode } equals new {CouncilCode=h.District, PostCode=h.ZipCode } select s;
这适用于任何类型的数据类型.
这是我能够让它工作的唯一方法(在c#中).
var qry = from t1 in table1 join t2 in table2 on new {t1.ID,t1.Country} equals new {t2.ID,t2.Country} ...
您可以将查询放在Where子句中,而不是使用join运算符.
join操作符支持VB.NET中的多个子句,但不支持C#.
或者,您可以使用ANSI-82样式的"SQL"语法,例如:
from t1 in table1 from t2 in table1 where t1.x == t2.x && t1.y == t2.y
来自http://www.onedotnetway.com/linq-to-sql-join-on-multiple-conditions/
这两个表都有PostCode和CouncilCode作为公共字段.假设我们想要从ShoppingMall中检索所有记录,其中PostCode和HouseCode on House匹配.这要求我们使用两列进行连接.在LINQ中,可以使用匿名类型完成连接.这是一个例子.
var query = from s in context.ShoppingMalls join h in context.Houses on new { s.CouncilCode, s.PostCode } equals new { h.CouncilCode, h.PostCode } select s;
var query = from s in context.ShoppingMalls join h in context.Houses on new {CouncilCode=s.CouncilCode, PostCode=s.PostCode } equals new {CouncilCode=h.District, PostCode=h.ZipCode } select s;
这适用于任何类型的数据类型.