我需要返回Customers
中與列ShipTo
不同的記錄的所有列。我試着用Distinct()
此查詢,但它返回重複記錄:Linq查詢返回基於不同列值的整個行
var query = (from o in Orders
from c in Customers
where (from x in CustomerOrders
where x.CustomerId == customerId
&& !x.OrderType.Equals('A')
select x.OrderId).Contains(o.OrderId)
&& c.CustomerId == customerId
&& c.ShipTo == o.ShipTo
&& !o.OrderStatus.Equals('C')
select c).Distinct();
我又試圖改寫使用Group By
和First()
查詢。我沒有收到任何語法錯誤,但查詢在使用LinqPad進行測試時會引發異常。
var query = (from o in Orders
from c in Customers
where (from x in CustomerOrders
where x.CustomerId == customerId
&& !x.OrderType.Equals('A')
select x.OrderId).Contains(o.OrderId)
&& c.CustomerId == customerId
&& c.ShipTo == o.ShipTo
&& !o.OrderStatus.Equals('C')
group c by c.ShipTo into g
select g.First());
「全部'Customer'列但通過「ShipTo」截然不同「聽起來像是一個矛盾。 – tinudu