开发者

Comparing 2 lists from different sources in C#

开发者 https://www.devze.com 2023-02-25 05:12 出处:网络
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

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 th开发者_开发百科e 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?


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 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).

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. (Meaning that no elements were added & no elements were removed).

0

精彩评论

暂无评论...
验证码 换一张
取 消