简体   繁体   中英

C# linq, obtain most frequent item in collection

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();

Try FirstOrDefault instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM