0

How would I do this in LINQ?

SQL

Where tblAccounts.AccountCode = tblAccountAssociations.ChildCode
And tblAccountAssociations.AssociationType = "DS"

Here is my attempt. The problem seems to be with "assoc.AssociationType == "DS". Is it part of the join or the Where clause?

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 };

Thanks in advance

4

3 回答 3

0
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 }; 
于 2012-06-04T05:40:49.720 回答
0

是的,“and”应该是“&&”,但你的问题的答案是,在 Linq 中,谓词assoc.AssociationType == "DS不是连接的一部分。

在 SQL 中,您可以将其作为连接语句的一部分

...
FROM tblAccounts c
INNER JOIN tblAccountAssociations a ON
    c.AccountCode = a.ChildCode AND a.AssociationType == "DS" 
...

但是在 Linq 语句中,您只需将其添加为谓词,就像您所做的那样(除了“和”问题)。

在 SQL 中,就执行计划(性能)而言,将其添加到 JOIN 短语中或放入单独的 WHERE 条件中都没有关系。

于 2012-06-05T07:18:17.330 回答
0

根据 MSDN (http://msdn.microsoft.com/en-us/library/bb311043.aspx),在“where”子句中使用“&&”而不是“and”来指定多个条件。

于 2012-06-01T21:49:44.283 回答