使用 LINQ 分离出两个相同的集合,除了不同的 id

Separate out two identical collections except for different ids with LINQ

我正在尝试生成一种方法,该方法输入两个数据库 (.db) 文件,将它们加载到一个包含各种字符串属性和唯一 ID 的对象中。

db 文件是从 LiteDB 实例创建的,具有根据 LiteDB 库方法分配的唯一 ID,如下所示:

Id = ObjectID.NewObjectId();

我正在尝试获取两个数据库集合之间所有不同的项目,但不幸的是,我尝试的任何 LINQ 查询都失败了,因为 ID 始终是唯一的,尽管对象中包含的所有其他值都是相同的。

我想执行一个 LINQ 查询,该查询将忽略对象中的 ID 字段,但对该对象中的所有其他属性进行比较,但我很难做到这一点

我最初的想法是使用

var differences = items1.Except(items2)

但是由于 ID 不同,这会将所有项目填充到差异集合中。

任何关于如何排除 ID 比较的建议都将不胜感激,该工具可能用于数百万条记录,因此需要高效。

Except 有一个 overload that accepts an IEqualityComparer<T>. You can implement this interface in a class in a way that ignores the ID and then pass an instance of that class to the method. See this example 如果您需要有关如何实施 IEqualityComparer<T>.

的参考