计算列表数组中的非空元素
本文关键字:元素 列表 数组 计算 | 更新日期: 2023-09-27 18:36:11
我在其他地方都找不到这个。我有一系列列表:
public List<xmldata>[] XMLArrayList = new List<xmldata>[9999];
要初始化列表并将其插入每个位置,我执行以下操作:
for(int m=0; m< XList.XMLArrayList.Count(); m++)
{
XList.XMLArrayList[m] = new List<xmldata>();
}
但我想数一数有多少元素不是空的。例如:位置 0 到 5 上有一个列表。但其他职位则不然。
尝试了 linq 方法:
int count = XList.XMLArrayList.Count(x => x != null);
但它向我返回数组大小 (9999)。如何计算列表数组上的非空元素?Ps:已经尝试过字典和列表列表 - 这种方法最适合实现我需要的。
谢谢。
试试这个:
int count = XList.XMLArrayList.Count(x => x.Count()>0);
你也可以这样做
XList.XMLArrayList.Where(x => x.Any()).Count();