[英]Use LINQ to Join on Multiple Fields
我将如何在LINQ中做到这一点?
SQL
Where tblAccounts.AccountCode = tblAccountAssociations.ChildCode
And tblAccountAssociations.AssociationType = "DS"
这是我的尝试。 问题似乎出在“ assoc.AssociationType ==” DS“上。它是联接还是Where子句的一部分?
var customers =
from customer in context.tblAccounts
join assoc in context.tblAccountAssociations on customer.AccountCode equals assoc.ChildCode
where customer.AccountType == "S" and assoc.AssociationType == "DS"
orderby customer.AccountType
select new { Customer = customer, Assoc = assoc };
提前致谢
根据MSDN(http://msdn.microsoft.com/zh-cn/library/bb311043.aspx),使用“ &&”而不是“ and”在“ where”子句中指定多个条件。
var customers =
from customer in context.tblAccounts
join assoc in context.tblAccountAssociations on customer.AccountCode equals assoc.ChildCode
where customer.AccountType == "S" **&&** assoc.AssociationType == "DS"
orderby customer.AccountType
select new { Customer = customer, Assoc = assoc };
是的,“和”应为“ &&”,但您的问题的答案是在Linq中谓词assoc.AssociationType == "DS
不是assoc.AssociationType == "DS
一部分。
在SQL中,您可以使其成为连接语句的一部分
...
FROM tblAccounts c
INNER JOIN tblAccountAssociations a ON
c.AccountCode = a.ChildCode AND a.AssociationType == "DS"
...
但是在Linq陈述中,您只是将其添加为谓词即可(除了“ and”问题)。
在SQL中,就执行计划(性能)而言,将其添加到JOIN短语中还是将其置于单独的WHERE条件中都没有关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.