If you override the equality of People then you can also use:
peopleList2.Except(peopleList1)
Except
should be significantly faster than the Where(...Any)
variant since it can put the second list into a hashtable. Where(...Any)
has a runtime of O(peopleList1.Count * peopleList2.Count)
whereas variants based on HashSet<T>
(almost) have a runtime of O(peopleList1.Count + peopleList2.Count)
.
Except
implicitly removes duplicates. That shouldn't affect your case, but might be an issue for similar cases.
Or if you want fast code but don't want to override the equality:
var excludedIDs = new HashSet<int>(peopleList1.Select(p => p.ID));
var result = peopleList2.Where(p => !excludedIDs.Contains(p.ID));
This variant does not remove duplicates.