LINQ Take语法

时间:2012-04-12 06:37:08

标签: linq-to-sql

我编写了一些LINQ来模拟SQL GroupBy语句(见下文)。但是,我还需要在执行组之前仅考虑最后10个settingIds。我想我会用Take来做这个,但是我的陈述中的正确语法是什么?

var settings2 = from s in dc.SystemSettings
                where s.campConfig.campaignType == campType
                      && s.campId != campId
                      && s.settingKey == ticket.setting.Key
                orderby s.settingId descending
                group s by s.settingValue
                into grp
                select new
                {
                    SettingValue = grp.Key,
                    SettingCount = grp.Select(x => x.settingValue).Count()
                };

1 个答案:

答案 0 :(得分:0)

我会做这样的事情

var settings2 = from sOuter in 
    (from s in dc.SystemSettings
    where s.campConfig.campaignType == campType
      && s.campId != campId
      && s.settingKey == ticket.setting.Key
    orderby s.settingId descending
    select s).Take(10)
    group sOuter by sOuter.settingValue
    into grp
    select new
    {
      SettingValue = grp.Key,
      SettingCount = grp.Select(x => x.settingValue).Count()
    };