0

我编写了一些 LINQ 来模拟 SQL GroupBy 语句(见下文)。但是,在分组之前,我也只需要考虑最后 10 个 settingId。我想我会使用 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()
                };
4

1 回答 1

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()
    };
于 2012-04-12T07:09:42.193 回答