简体   繁体   English

比较来自C#中不同来源的2个列表

[英]Comparing 2 lists from different sources in C#

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). 我的程序是一个同步程序,每2分钟将数据从源A同步到源B-现在无论如何它都会添加新行,但是显然这对于​​生产环境而言并不理想,因此我希望能够查看是否源A中的行与源B中的行相同(来自最新同步)。 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. 因此,我定义了一个结构,该结构包含所有存储的字段( 除了在源之间不匹配的任何PK字段),并且在执行同步而不是直接同步到源B时,我创建了该结构的列表并将结果放在那里。 Then I create a new instance of a list of that struct, and put the most recent sync results from Source B in there. 然后,我创建该结构列表的新实例,并将来自源B的最新同步结果放入其中。

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? 但是我将如何比较这两个列表?

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 . 但是,如果无论顺序如何都需要使用列表,则可以使用Enumerable基于集合的操作。 If you have collections old and new , you can get a list of elements that are in the new collection, but not in the original one using new.Except(old) (see MSDN documentation for Except ). 如果有oldnew集合,则可以使用new.Except(old)获得新集合中的元素列表,而原始集合中没有(请参阅MSDN文档以获取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) and new.Except(old) ) should be both zero. 如果要检查两个集合是否包含完全相同的元素,则两个差异集( old.Except(new)new.Except(old) )的大小都应为零。 (Meaning that no elements were added & no elements were removed). (意味着没有添加任何元素,也没有删除任何元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM