开发者

best way to compare differences between lists

I have two sets of lists below:

List 1 Info

ID = 1

PersonFirstName = Sarah

PersonLastName = Smith

PersonFavoriteFood = Pizza

PersonFavoriteColor = Purple PersonFavoriteFlower = Lily

List 2 Info

ID = 1

PersonFirstName = Sarah

PersonLastName = Smith

PersonFavoriteFood = Hamburger

PersonFavoriteColor = Yellow

PersonFavoriteFlower = Rose

As you can see - Sarah Smith came into some system and changed her favorite food, favorite color and favorite flower. If her manager were to come into this app and open it up he would see three rows in the history of Sarah Smith's record:

Date Changed = 2/16

Field Changed = PersonFavoriteFood

Original Value = Pizza

Changed Value = Hamburger

Changed By = Sarah Smith

Date Changed = 2/16

Field Changed = PersonFavoriteColor

Original Value = Purple

Changed Value = Yellow

Changed By = Sarah Smith

Date Changed = 2/16

Field Changed = PersonFavoriteFlower

Original Value = Lily

Changed Value = Rose

Changed By = Sarah Smith

The threee rows he sees need to be saved in the database in that way. So thats one table with two rows (original row and changed row) and another table with as many rows as there were changes and what those changes were (food, color, flower). I can easily get the first table with the original and changed row. I am suck in my VB.NET code going through LINQ on these two lists (the original and changed). My thought is to select value from each column in each list, determine if it is different, if it is then save it to other table, if it isn't then get the next column from each list and compare. B开发者_开发百科ut is there an easier way?

My link code is:

Dim columnvalue1 = from a in OriginalList select a.column1
Dim columnvalue2 = from b in ChangedList select a.column1

If columnvalue1 = columnvalue2 then
SaveDifferencesToTable(colomnvalue1, columnvalue2)
End If

If there is a better way even if it is outside of LINQ please let me know. I can do it this way but I worry that it is the wrong way to do it. I am using entity framework as well.


You can use Enumerable.Except method to create a set of differences. You can also implement your own comparer if you have to. In anyway, I am sure you will have to write some code.

Here is more details on the method:

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜