2

我有这个 SQL 查询可以做我想做的事,但我对如何GroupBy()OrderBy()工作的确切方式感到困惑......

这是我拥有的运行良好的 SQL 语句:

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

理想的最终产品是满足这些条件的 UserID 列表,按每个 SkillID 的出现次数排序,其中具有最多 SkillID 匹配的用户位于顶部,向下到具有较少匹配的用户。

我试图无济于事:

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

1 回答 1

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);
于 2013-05-03T16:10:21.680 回答