2012-04-12 47 views
0

我寫了一些LINQ來模擬SQL GroupBy語句(請參見下文)。不過,在做羣組之前,我也只需要考慮最後的10個設置ID。我想我會用Take來做到這一點,但是在我的陳述中,正確的語法是什麼?LINQ採用語法

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() 
       }; 
+0

能否請您縮進代碼更好地使其更具可讀性? – 2012-04-12 07:11:03

回答

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() 
    };