排序一个Iqueryable

时间:2009-12-09 01:05:10

标签: linq-to-sql iqueryable

我有一个IQueryable有重复的条目,我想按出现次数对这个IQueryable进行排序。

2 个答案:

答案 0 :(得分:3)

试试这个:

from e in myQueryable
group e by e.Something into g
order by g.Count()
select g  //Or, g.First()

答案 1 :(得分:1)

像这样:

list
    .GroupBy(x => x.ID)
    .Select(x => new {Obj = x.First(), Count = x.Count()})
    .OrderBy(x => x.Count)
    .Select(x => x.Obj);

我更喜欢SLaks的解决方案。

list
    .GroupBy(x => x.ID)             
    .OrderBy(x => x.Count())
    .Select(x => x.First());