首页 > 解决方案 > 我无法在 foreach 循环内的列表中添加项目如何解决?

问题描述

我试图在 foreach 循环内的 myList 中添加一个项目:我总结了问题,让我们考虑 EmployeeInfo 有 1000 条记录,然后 HasValue 没有添加项目。

List<bool> HasValue = new List<bool>();

foreach(var list in EmployeeInfo)
{
 if(list.Count() > 0)
   {
     HasValue.Add(true);
   }
  else 
   {
     HasValue.Add(false);
   }

}

标签: c#asp.net-web-api

解决方案


这是否有助于您更接近您的答案?

    public class EmployeeInfo {
        public List<String> TheList { get; } = new List<String>();
    }

    public void Test(){
        var employeeInfo = new EmployeeInfo();
        var hasValues = employeeInfo.TheList.Count( list => list.Any() );
        var noValues = employeeInfo.TheList.Count - hasValues;
    }

推荐阅读