Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this questionI have a Queue. How do I select the value that occurs most frequently and a开发者_运维问答ssign it to an int via LINQ expressions?
int number = (from i in queue
group // ?
select i).First();
int number = queue.GroupBy( x => x)
.OrderByDescending( g => g.Count())
.Select( g => g.Key)
.First();
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
queue.Enqueue(4);
queue.Enqueue(5);
queue.Enqueue(2);
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(4);
int number =(from c in queue
group c by c into g
orderby g.Count() descending
select g.Key).FirstOrDefault();
I hope it can help you.
精彩评论