我有一个类,我使用list来获取(在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;
}
如何累积添加列表值?
答案 0 :(得分:2)
我认为您想要做的是以下内容:
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));
答案 1 :(得分:1)
您可以使用.Add()将内容添加到列表中,这是累积的每个定义。