此代码的作用
IEnumerable<Boat> newBoats = updatedBoats.Where(c=> oldBoats.Any(d =>d.GetId() != c.GetId()).ToList()
大致翻译为:“给我所有更新的船,其ID与至少一个旧船ID不匹配”。那不是你想要的。您想要的逻辑是“给我所有ID与旧船ID不匹配的更新船”,这是由@zeroef正确指定的:
var newBoats = updatedBoats.Where(u => !oldBoats.Any(o => o.GetId() == u.GetId()));
// This is O(o * n) for # of old boats * # of updates that are new boats, and something like O((o/2)*n) for # of old boats * number of updated old boats
尽管如此,请注意我的评论。使用
HashSet<T>
:
// This is O(n) for # of updated boats
var newBoatIds = new HashSet<Int32>(updatedBoats.Select(b => b.GetId()));
// This is O(n) for # of old boats
newBoatIds.ExceptWith(oldBoats.Select(b => b.GetId()));
这显著减少了嵌套迭代的数量,如果您有很多船(尤其是更新中有很多新船),您将看到不同。
HashSet方法适用于ID,但如果使用ID在Boat类上实现Equals()和GetHashcode()进行比较,则也可以使其适用于实体本身。