views:

56

answers:

2

I have an IQueryable with duplicate entries and I want to sort this IQueryable by the count of occurrences.

+2  A: 

Try this:

from e in myQueryable
group e by e.Something into g
order by g.Count()
select g  //Or, g.First()
SLaks
cool it works..!
Ashish
is there anyway to get the maximum occurring element first than the least.
Ashish
never mind got it..
Ashish
+1  A: 

Like this:

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

I like SLaks' solution more, though.

list
    .GroupBy(x => x.ID)          
    .OrderBy(x => x.Count())
    .Select(x => x.First());
Mike Chaliy
I do not see a problem here, anyways fixed :).
Mike Chaliy
I did not mean to imply that there was a problem.
SLaks
Sorry, probably misunderstood you..
Mike Chaliy