在 C# 中比较来自不同来源的 2 个列表
我的程序是一个同步程序,每 2 分钟将数据从源 A 同步到源 B - 现在它无论如何都会添加新行,但显然这对于生产环境来说并不理想,所以我希望能够检查是否源 A 中的行与源 B 中的行相同(来自最近的同步)。如果是,则这次不执行此同步。
因此,我定义了一个结构体,其中包含存储的所有字段(除了源之间不匹配的任何 PK 字段),并且当执行同步时,而不是直接同步到源 B,我创建了一个结构列表并将结果放入其中。然后,我创建该结构列表的一个新实例,并将源 B 的最新同步结果放入其中。
因此从理论上讲,如果自上次同步以来没有任何变化,那么除了顺序之外,这两个列表应该是相同的。但我该如何比较这两个列表呢?
My program is a sync program that synchronizes data from Source A to Source B every 2 minutes - now currently it adds new rows regardless, but obviously this isn't ideal for a production environment so I want to be able to check to see if the rows in Source A are identical to the rows in Source B (from the most recent sync). If they are, do not perform this sync this time.
So I've defined a struct which contains all the fields stored (except any PK fields which won't match between the sources), and when the sync is performed, rather than sync straight to Source B, I create a list of the struct and put the results in there. Then I create a new instance of a list of that struct, and put the most recent sync results from Source B in there.
So in theory, if nothing's changed since the last sync, then the 2 lists should be identical, apart from the order. But how would I go about comparing these two lists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不清楚问题到底是什么。
但是,如果您需要使用列表(无论顺序如何),则可以使用
Enumerable
中基于集合的操作。如果您有集合old
和new
,您可以使用new.Except( 获取新集合中但不在原始集合中的元素列表旧)
(请参阅 MSDN 文档了解除外)。如果要检查两个集合是否包含完全相同的元素,则两个差异集的大小(
old.Except(new)
和new.Except(old)
) 应该都为零。 (意味着没有添加任何元素,也没有删除任何元素)。It is not clear to me what exactly the question is.
However, if you need to work with lists regardless of the order, you can use set-based operations from
Enumerable
. If you have collectionsold
andnew
, you can get a list of elements that are in the new collection, but not in the original one usingnew.Except(old)
(see MSDN documentation for Except).If you want to check if the two collections contain exactly the same elements, then the sizes of the two difference sets (
old.Except(new)
andnew.Except(old)
) should be both zero. (Meaning that no elements were added & no elements were removed).