为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
繁华开满天机 2023-09-24 10:45:34
我有一个嵌套列表,如下所示: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 获取此输出吗?
查看完整描述

1 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

这应该有效:

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


查看完整回答
反对 回复 2023-09-24
  • 1 回答
  • 0 关注
  • 53 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信