交集 aList.Intersect(bList)
不行呢,出来的是多的那个集合
@淘@淘:
交集 和差集 应该行,后面就是操作这个 交集或者差集而已
@淘@淘: 楼主的意思不是取交集吗/? Intersect 就是取两个集合的交集
先遍历第一个list,然后用linq在第二个list中查询
foreach (var item in aList) { var target = bList.Find(m => m.ID == item.ID); if (target == null) continue; bList.Remove(target); }
可是当aList.Count<bList.Count的时候那后边的那些数据就判断不了了
class User { public string Id; public string Name; } private void RemoveInexistence(List<User> aList, List<User> bList) { if (aList != null && aList.Count > 0 && bList != null && bList.Count > 0) { List<User> clonedList = new List<User>(); foreach (User u in aList) { clonedList.Add(u); } foreach (User us in clonedList) { User user = bList.Find(c => c.Id == us.Id); if (user == null) { aList.Remove(aList.Find(c=>c.Id==us.Id)); } } } } private void Test() { List<User> aList = new List<User>(); User u1 = new User(); u1.Id = "1"; u1.Name = "aaa"; aList.Add(u1); User u2 = new User(); u2.Id = "2"; u2.Name = "bbb"; aList.Add(u2); List<User> bList = new List<User>(); bList.Add(u2); User u3 = new User(); u3.Id = "3"; u3.Name = "ccc"; bList.Add(u3); this.RemoveInexistence(aList, bList); int iCount = aList.Count; }
您这个跟楼上的大侠的意思差不多,当aList.Count<bList.Count的时候那后边的那些数据就判断不了了