2009-09-18 156 views
0

會是什麼LINQ表達式相當於以下TSQL查詢:LINQ表達式相當於TSQL查詢

SELECT c.[CustomerId] 
    ,c.[Name] 
    , (SELECT COUNT(*) FROM Incidents WHERE CustomerId = c.CustomerId) AS IncidentsCount 
    , (SELECT COUNT(*) FROM Opportunities WHERE CustomerId = c.CustomerId) AS OpportunitiesCount 
    , (SELECT COUNT(*) FROM Visits WHERE CustomerId = c.CustomerId) AS VisitsCount 
FROM [Customers] c 

回答

3

我沒有仔細檢查這在Visual Studio但這應該工作:

var x = (from c in Context.Customers 
     select new { 
      CustomerId = c.CustomerId, 
      Name = c.Name, 
      IncidentsCount = 
       Context.Customers.Count(i => i.CustomerId == c.CustomerId), 
      OpportunitiesCount = 
       Context.Opportunities.Count(o => o.CustomerId == c.CustomerId), 
      VisitsCount = 
       Context.Visits.Count(v => v.CustomerId == c.CustomerId) 
     }); 

更新:我改變了代碼,使其更容易閱讀。

+0

非常好,謝謝! – desautelsj 2009-09-19 10:46:47