Linq to sql linq到sql:从同一个表连接多个列
如何通过Linq内部连接同一表中的多个列 例如: 我已经有了这个Linq to sql linq到sql:从同一个表连接多个列,linq-to-sql,join,Linq To Sql,Join,如何通过Linq内部连接同一表中的多个列 例如: 我已经有了这个 join c in db.table2 on table2.ID equals table1.ID 我需要加上这个 join d in db.table2 on table2.Country equals table1.Country 在VB中: 这是我能让它工作的唯一方法(用c#) 您可以将查询放在Where子句中,而不使用join运算符 join运算符支持VB.NET中的多个子句,但不支持C# 或者,您可以使用ANSI-
join c in db.table2 on table2.ID equals table1.ID
我需要加上这个
join d in db.table2 on table2.Country equals table1.Country
在VB中:
这是我能让它工作的唯一方法(用c#)
您可以将查询放在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
从
这两个表的公共字段都是PostCode和CouncilCode。假设我们想从ShoppingMall中检索所有记录,其中房屋上的邮政编码和议会编码都匹配。这要求我们使用两列进行连接。在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;
这适用于任何类型的数据类型。不知道使用查询语法查找多列linq联接的示例需要多少时间。谢谢。这种逻辑结构在sql中被弃用了将近20年。()在linq中匹配它并没有错,特别是在像这样的小查询中,但是Pete Haas的语法在风格上更可取
from t1 in table1
from t2 in table1
where t1.x == t2.x
&& t1.y == t2.y
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;