views:

237

answers:

1

Hi, i am trying to obtain an item from a collection that occurs most frequently. if i were in SQL i would do something like this..

select top(1) extension from database.table
group by extension
order by count(extension) desc

but im trying to do this using linq.

Can someone assist with the translation?

so far i have this but its not working...

string topExtension = (from c in extensions select c).GroupBy(d => d).OrderByDescending(e => e.Count()).SingleOrDefault();
+2  A: 

Try FirstOrDefault instead.

EDIT: Oops, sorry. Try .FirstOrDefault().Key.

mquander
same error... Cannot implicitly convert type 'System.Linq.IGrouping<string,string>' to 'string'
Grant
ahh that worked.. thanks!
Grant
You should use .First() here .FirstOrDefault() can return null, which will cause a NullReferenceException on .Key.
Bryan Watts