[英]LINQ to SQL - Left OUTER Join with two joins not working
In the following LINQ Query I need to display All
the customers including the customers that have not placed any order but whose order is priced more than $100. 在下面的LINQ查询中,我需要显示
All
客户,包括尚未下任何订单但订单价格超过$ 100的客户。 But my following LINQ Query is returning all the customers regardless of their order price. 但是我接下来的LINQ查询正在返回所有客户,无论他们的订单价格如何。 It seems to be ignoring my
Where(ord => ord.price > 100)
clause in the LINQ query below. 似乎在下面的LINQ查询中忽略了我的
Where(ord => ord.price > 100)
子句。 What I may be doing wrong? 我可能做错了什么?
Models : 型号
public class Customer
{
public int CustomerId { get; set; }
public string CustName{ get; set; }
}
public class Order
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
public float price { get; set; }
}
LINQ Query : LINQ查询 :
var Query1 = from c in Customers
join ord in Orders on c.CustomerId equals ord.CustomerId into cord into cord
from t in cord.Where(ord => ord.price > 100).DefaultIfEmpty()
select new {CustName= c.Name, OrderID = (t == null ? 0 : t.OrderId)};
SQL Query : SQL查询 :
I want to translate following T-SQL query into LINQ query: 我想将以下T-SQL查询转换为LINQ查询:
SELECT c.Name, OrderID
FROM Customers c
LEFT OUTER JOIN Orders ord
ON c.CustomerID = ord.CustomerID
AND ord.Price > 100
Well, I don't have your data to be able to verify this, but one issue that jumps out at me is you're not specifying the field (CustomerId) on which to join the two collections. 好吧,我没有您的数据可以验证这一点,但是让我惊讶的一个问题是,您没有指定要加入两个集合的字段(CustomerId)。
Try modifying your query like this: (adding on c.CustomerId equals ord.CustomerId
) 尝试像这样修改查询:(
on c.CustomerId equals ord.CustomerId
)
var Query1 = from c in Customers
join ord in Orders on c.CustomerId equals ord.CustomerId into cord
from t in cord.DefaultIfEmpty()
where t.price > 100
select new {CustName = c.Name, OrderID = (t == null ? 0 : t.OrderId)};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.