2013-05-03 77 views
2

我有這樣的SQL查詢,做什麼,我也想,但我對如何GroupBy(),準確OrderBy()工作...的GroupBy和排序依據的LINQ

這裏是SQL語句我有工作正常困惑:

SELECT TOP 5 UserID, COUNT(*) 
    FROM SkillLevelMap 
    WHERE SkillID = 183 OR SkillID = 102 OR SKILLID = 103 
    GROUP BY UserID 
    ORDER BY COUNT(*) DESC 

什麼最終產品的理想情況下,是滿足這些條件的用戶ID的列表,每個SkillID,其中有相匹配的最SkillIDs用戶在頂部的ocurrence有序,下到人那少了。

我試圖無濟於事:

var userList2 = SQEPData.AllSkillLevelMaps.Where(x => skillIDs.Contains(x.SkillID)).GroupBy(g => g.User); 

回答

2

您的查詢似乎是類似於:

var userList2 = SQEPData.AllSkillLevelMaps 
        .Where(x => skillIDs.Contains(x.SkillID)) 
        .GroupBy(x => x.User) 
        .Select(g => new { User = g.Key, Count = g.Count() }) 
        .OrderByDescending(i => i.Count) 
        .Take(5);