c# - 使用 LINQ 获取嵌套列表的计数列表

标签 c# .net list linq count

我有一个嵌套列表,如下所示:

List<List<int>> nestedList = new List<List<int>>();

示例数据如下: {{1,2,3}, {4,4,2,6,3}, {1}}

所以我想对每个列表进行计数,并将值获取到另一个列表。例如,输出应为:{3,5,1}

我试过使用 foreach:

List<int> listCount = new List<int>();
foreach (List<int> list in nestedList)
{
    listCount.Add(list.Count());
} 

我可以知道如何使用 LINQ 获得此输出吗?

谢谢!

最佳答案

这应该有效:

var result = nestedList.Select(l => l.Count).ToList();

关于c# - 使用 LINQ 获取嵌套列表的计数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651767/

相关文章:

c# - 在 Oracle 中生成 .Net 报价

python - 比较两个列表,保留重复项但删除 Python 中索引位置中的相同项

c# - 在事件处理 C# 中创建事件变量需要什么?

c# - 如何在 Entity Framework 中添加种子方法

.NET:可执行文件应该进行强名称签名吗?私有(private) DLL 怎么样?

c# - 如何创建一个dll以在单独的项目中引用和使用?

list - 检查 clisp 中的模式列表

scala - 删除给定列表的给定数量的正项

c# - 使用 Azure SDK 将多个地址前缀添加到网络子网

c# - 如何在 C# 中使用 SSL 连接到 LDAP Novell?