1

我不知道如何在 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
4

2 回答 2

2

严重地?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()
}
于 2011-12-02T20:27:50.030 回答
0
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)
于 2011-12-02T20:26:08.270 回答