【发布时间】:2018-02-09 14:20:41
【问题描述】:
我有一个整数列表:
List<int> intList = new List<int>();
intList.Add(23);
intList.Add(53);
intList.Add(98);
我有一个 Employee 对象列表:
List<Employee> employeeList = new List<Employee>();
employeeList.Add(m1);
employeeList.Add(m2);
employeeList.Add(m3);
employeeList.Add(m4);
employeeList.Add(m5);
employeeList.Add(m6);
employeeList.Add(m7);
Employee 类型的每个对象都有 3 个属性:
int Age;
string Name;
string Gender;
现在,我有包含 3 个项目的列表 intList,包含 7 个对象的列表 employeeList。 从employeeList 列表中,我想完全删除所有具有Age 属性的Employee 与列表intList 中存在的任何值都不同。
如何以有效的方式做到这一点?
例如,如果m4.Age=2" 和m6.Age=98 以及所有其他员工的年龄不同,
在阐述结束时,我希望我的employeeList 在位置0 和1 中只包含m4 和m6。
谁能帮我解决这个问题?
【问题讨论】:
-
为什么你的整数是字符串?
-
@GolezTrol 可能是因为
Employee.Age也是string?当然,这也是值得怀疑的。 -
List<int> intList = new List<string>();甚至无法编译。 -
不,你没有整数列表,因为我认为你做不到
List<int> intList = new List<string>(); -
employeeList.RemoveAll(e => !intList.Contains(e.Age))