开发者

How do I select the value that occurs most frequently in queue via LINQ? [closed]

开发者 https://www.devze.com 2023-04-11 21:59 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question

I 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消