使用Linq返回具有最大计数的列表 [英] Return List with Maximum Count using Linq

查看:84
本文介绍了使用Linq返回具有最大计数的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#和Linq我将如何返回具有最大大小/计数的List< ....>?

Using C# and Linq how would i return the List<....> with the largest size / count?

推荐答案

它听起来听起来好像您有n个列表,并且希望从中选择一个计数最大的列表.

It sounds as if you have n Lists, and you wish to single out the one with the largest count.

尝试一下:

List<int> ints1 = new List<int> { 10, 20, 30 };
List<int> ints2 = new List<int> { 1, 2, 3, 4 };
List<int> ints3 = new List<int> { 100, 200 };

var listWithMost = (new List<List<int>> { ints1, ints2, ints3 })
                   .OrderByDescending(x => x.Count())
                   .Take(1);

您现在拥有的元素数量最多的列表.考虑这样一种情况,其中有2个以上的列表具有相同的最大元素数.

You now have the List with the most number of elements. Consider the scenario where there are 2+ lists with the same top number of elements.

这篇关于使用Linq返回具有最大计数的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆