比较两个集合
我有一个似乎常见的问题/模式。同一对象的两个集合。该对象具有许多属性和其中的一些嵌套对象。汽车有一个名为 id 的属性,它是唯一标识符。
我想找到 LINQ 方法来进行比较,其中包括:
- 一个集合中的项目,而不是另一个集合中的项目(反之亦然)
- 对于匹配的项目,是否有任何更改(更改将是所有属性的比较?(我只关心可设置的属性,我会为此使用反射吗?)
i have what seems like a common problem / pattern. two collections of the same object. The object has a number of properties and some nested objects within it. Car has a property called id which is the unique identifier.
I want to find the LINQ way to do a diff, which includes:
- Items in one collection and not the other (visa versa)
- For the items that match, are there any changes (changes would be a comparison of all properties? (i only care about settable properties, would i use reflection for this ?? )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Enumerable.Except()
方法。这使用比较器(默认的或您提供的比较器)来评估哪些对象同时存在于两个序列中或仅存在于一个序列中:如果要对两个序列执行完全析取
(AB) union (BA)
,您必须使用:如果您有一个复杂类型,您可以为您的类型 T 编写一个
IComparer
并使用接受比较器的重载。对于问题的第二部分,您需要滚动自己的实现来报告类型的哪些属性不同。.NET BCL 中没有直接内置任何内容。您必须决定该报告将采取什么形式?您如何识别和表达复杂类型中的差异?您当然可以为此使用反射......但如果您只处理单一类型,我会避免这种情况,并为其编写一个专门的差异实用程序。如果您要支持一系列类型,那么反射可能更有意义。
You can use the
Enumerable.Except()
method. This uses a comparer (either a default or one you supply) to evaluate which objects are in both sequences or just one:If you want to perform a complete disjunction of both sequences
(A-B) union (B-A)
, you would have to use:If you have a complex type, you can write an
IComparer<T>
for your type T and use the overload that accepts the comparer.For the second part of your question, you would need to roll your own implementation to report which properties of a type are different .. there's nothing built into the .NET BCL directly. You have to decide what form this reporting would take? How would you identify and express differences in a complex type? You could certainly use reflection for this ... but if you're only dealing with a single type I would avoid that, and write a specialized differencing utility just for it. If yo're going to support a borad range of types, then reflection may make more sense.
您已经收到了上半场的出色答复。正如 LBushkin 所解释的,后半部分不能直接由 BCL 课程完成。这是一个简单的方法,它遍历所有公共可设置属性(注意:在这些情况下,获取器可能不是公共的!)并将它们一一比较。如果两个对象 100% 相等,则返回 true。否则,它会提前爆发并返回 false:
您可以轻松地将此方法添加到标准 LINQ 表达式中,如下所示:
You've already received an excellent answer for your first half. The second half, as LBushkin explains, cannot be done by BCL classes directly. Here's a simple method that goes through all public settable properties (note: it is possible that the gettor, in these cases, is not public!) and compares them one by one. If two objects are 100% equal, it will return true. Else, it will break out early and return false:
You could easily add this method to a standard LINQ expression, like this: