Làm cách nào để tôi thực hiện điều này trong LINQ?LINQ; làm thế nào để có được kỷ lục với ngày tối đa với tham gia?
select
*
from customer c
left join order o on o.CustomerID = c.CustomerID
where o.OrderDate = (select MAX(OrderDate) from order where CustomerID = o.CustomerID)
không phải lo lắng về việc làm dups vì sẽ luôn có một đơn đặt hàng mỗi ngày.
tôi đã như xa như trái tham gia trong LINQ, nhưng không chắc chắn làm thế nào hoặc nơi để đặt các subquery trong
var query = from customer in clist
from order in olist
.Where(o => o.CustomerID == customer.CustomerID)
select new {
customer.CustomerID,
customer.Name,
customer.Address,
Product = order != null ? order.Product : string.Empty
};
giải pháp cuối cùng:.
var query = from customer in clist
from order in olist
.Where(o => o.CustomerID == customer.CustomerID && o.OrderDate ==
olist.Where(o1 => o1.CustomerID == customer.CustomerID).Max(o1 => o1.OrderDate)
)
select new {
customer.CustomerID,
customer.Name,
customer.Address,
order.Product,
order.OrderDate
};
Một giải pháp mà không bất kỳ lambdas
var query = from customer in clist
from order in olist
where order.CustomerID == customer.CustomerID && order.OrderDate ==
(from o in olist
where o.CustomerID == customer.CustomerID
select o.OrderDate).Max()
select new {
customer.CustomerID,
customer.Name,
customer.Address,
order.Product,
order.OrderDate
};
Xin cảm ơn, giải pháp cuối cùng của tôi dựa trên sự chuyển đổi theo nghĩa đen. – mfc