2012-05-08 34 views
10

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

Trả lời

13

Đây là bản dịch ít nhiều theo nghĩa đen

var query = from customer in clist 
      from order in olist 
       .Where(o => o.CustomerID == customer.CustomerID && 
          o.OrderDate == olist 
           .Where(o => o.CustomerID == customer.CustomerID) 
           .Select(o => o.OrderDate).Max()) 
      select new { 
       customer.CustomerID, 
       customer.Name, 
       customer.Address, 
       Product = order != null ? order.Product : string.Empty 
      }; 

nhưng tôi sẽ viết lại để

var query = from customer in clist 
      from order in olist 
       .Where(o => o.CustomerID == customer.CustomerID) 
       .OrderByDescending(o => o.OrderDate).Take(1) 
      select new { 
       customer.CustomerID, 
       customer.Name, 
       customer.Address, 
       Product = order != null ? order.Product : string.Empty 
      }; 
+0

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

1

làm việc như thế này cho bạn?

var query = 
    from customer in clist 
    join order in olist 
     on customer.CustomerID equals order.CustomerID 
     into orders 
    select new 
    { 
     customer.CustomerID, 
     customer.Name, 
     customer.Address, 
     Product = orders 
      .OrderByDescending(x => x.OrderDate) 
      .Select(x => x.Product) 
      .FirstOrDefault() ?? String.Empty 
    }; 
Các vấn đề liên quan