累计添加List输出

本文关键字:输出 List 添加 | 更新日期: 2023-09-27 18:14:06

我有一个类,我使用列表来获取(在c#中分配多个列表条目值)。

// This will return me more than 1 records.
List<ClassEmpInfo> employeeDetails =GetEmployeeInformation(); 
List<ClassEmployee> empInfo = null; 
foreach (ClassEmployee employee in employeeDetails) 
{
  //This needs to show all the ids belonging to employeeDetails.
  //If there are 3 different employee ids , 
  //the list empInfo should hold the output of all the 3, 
  //but i am getting the last 3rd one alone. 
  //How to cumulatively add 3 different employee ids.
  empInfo = GetEmployeeDetails(Id, EmpId); 
}

我正在获取最后的员工信息,而不是所有 empInfo列表中的员工详细信息。

如果类型是字符串,我可以这样做:

if (strType.Length > 0) 
{  
  strType = strType + returned values; 
} 
else
{  
  strType = strType; 
}

如何累计添加列表值?

累计添加List输出

我认为你应该这样做:

List empInfo = new List<detail_type>(); // whatever type is returned by GetEmployeeDetails
foreach (ClassEmployee employee in employeeDetails)
{
    empInfo.Add(GetEmployeeDetails(id, EmpId));
}

这对我来说相当不清楚,但是,如果GetEmployeeDetails返回一个值或一个值列表。如果它返回一个值列表,则将循环中的行更改为:

empInfo.AddRange(GetEmployeeDetails(id, EmpId));

您可以使用. add()向列表添加内容,这是按定义累积的