【发布时间】:2019-05-07 03:17:28
【问题描述】:
使用foreach,我想遍历两个不同的列表并找到相同的ID,然后删除该项目。
例如:
IList<Student> studentList1 = new List<Student>
{
new Student() { StudentID = 1, StudentName = "John" },
new Student() { StudentID = 2, StudentName = "Steve" },
new Student() { StudentID = 3, StudentName = "Bill" }
};
IList<Student> studentList2 = new List<Student>
{
new Student() { StudentID = 3, StudentName = "Bill" },
new Student() { StudentID = 4, StudentName = "Ram" },
new Student() { StudentID = 5, StudentName = "Ron" }
};
如何循环遍历studentList1 和studentList2,然后删除studentList2 中StudentID = 3 的对象?
【问题讨论】:
-
你可以使用LINQ方法
Where,或者List的方法Remove -
请具体
then remove that item从两个列表或一个列表中删除它,或者选择我们最喜欢的 -
抱歉,从 studentList2 中删除该项目
-
有没有办法使用foreach循环?