我不知道如何在 LINQ 中使用嵌套选择。如何将此 SQl 表达式转换为 LINQ?
Select i.ID, i.Impression,
(Select COUNT(ImpressionsId)
from DiaryImpressions
where DiaryPostsId = '2' AND ImpressionsId = i.ID) as Num from Impressions i
我不知道如何在 LINQ 中使用嵌套选择。如何将此 SQl 表达式转换为 LINQ?
Select i.ID, i.Impression,
(Select COUNT(ImpressionsId)
from DiaryImpressions
where DiaryPostsId = '2' AND ImpressionsId = i.ID) as Num from Impressions i
严重地?DiaryPostsId 是一个字符串?那好吧...
from i in context.Impressions
select new {
i.ID,
i.Impressions,
Num = (from d in context.DiaryImpressions
where d.DiaryPostsId == "2"
&& d.ImpressionsId == i.ID
select d).Count()
}
from ...
select new {
i.Id,
i.Impression,
Count = context.DiaryImpressions.Count(d => d.DiaryPostsId == 2 && d.ImpressionsId == i.Id)
}
如果正确映射对象,则可以直接使用子关系:
Count = i.DiaryImpressions.Count(d => d.DiaryPostsId == 2)