2012-05-08 8 views
10

मैं LINQ में यह कैसे कर सकता हूं?LINQ; जुड़ने के साथ अधिकतम तिथि के साथ रिकॉर्ड कैसे प्राप्त करें?

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) 

डुप्लिकेट के बारे में चिंतित नहीं है क्योंकि हमेशा प्रति दिन केवल एक आदेश होगा।

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

किसी भी lambdas

बिना एक अन्य समाधान:

मैं जहाँ तक छोड़ दिया के रूप में यकीन है कि कैसे या जहाँ में सबक्वेरी डाल करने के लिए अंतिम समाधान मिल गया LINQ में शामिल होने के लिए, लेकिन नहीं

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

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

उत्तर

13

यह कम या ज्यादा एक शाब्दिक अनुवाद

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

लेकिन मैं

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

धन्यवाद, मेरा अंतिम समाधान शाब्दिक परिवर्तन पर आधारित है। – mfc

1

को फिर से लिखने होगा कैसे आप के लिए यह काम करता है?

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