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
答案 0 :(得分:3)
我没有在视觉工作室中对此进行双重检查,但这应该有效:
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)
});
更新:我将代码更改为更容易阅读。