2011-06-08 41 views
3

我有麻煩的代碼轉換爲擴展方法的語法:轉換LINQ查詢表達式320交織成的擴展方法的語法

var query = from c in _context.Customers 
         from o in c.Orders 
         where o.DateSent == null 
         select new CustomerSummary 
         { 
          Id = c.Id, 
          Username = c.Username, 
          OutstandingOrderCount = c.Orders.Count 
         }; 

任何想法?

+1

ReSharper的可轉換到/從自動方法鏈,以防萬一你不知道。 – 2011-06-08 16:22:43

+1

所以可以http://www.linqpad.net – 2011-06-08 16:28:03

+1

你確定這個查詢做你想要的嗎?我假設您只需要爲每個客戶創建一個記錄,其中的訂單數量爲null的SentDate。 – NerdFury 2011-06-08 16:36:47

回答

2
var query = _context.Customer 
    .Where(c => c.Orders.Any(o => o.DateSent == null)) 
    .Select(c => new CustomerSummary 
    { 
    Id = c.Id, 
    Username = c.Username, 
    OutstandingOrderCount = c.Orders.Count(o => o.DateSent == null) 
    }; 
1

試試這個:

 var query = 
      _context.Customers.SelectMany(c => c.Orders, (c, o) => new {c, o}).Where(@t => o.DateSent == null) 
       .Select(@t => new CustomerSummary 
           { 
            Id = c.Id, 
            Username = c.Username, 
            OutstandingOrderCount = c.Orders.Count 
           });