2009-09-18 177 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