user478636
user478636

Reputation: 3424

Comparing two Lists

Both List A and List

List A = new List(); List B = new List();

For the sake of simplicity, I have mentioned the Products in each List. Te numbers here are referring to the Product ID.

  List A contains 1,2,3,4,5

  List B contains 1,2,3,4,5,6,7

I want to know the code in c# asp.net that will compare the two lists, and synchronizing them. Meaning if List B had some more Products (lets say 19,20) it copy 19,20 to List A.

Lets say it was a List, how would I compare the qty attribute. I have to check if the same product ID is in both lists. Then it would check the qty attribute. It would synch them both.

Upvotes: 1

Views: 1327

Answers (2)

Dan Puzey
Dan Puzey

Reputation: 34200

Not sure whether this is the most efficient solution (I'm sure there must be a better Linq method for this )...

c = A.Except(B).Union(B.Except(A)).ToList();

Upvotes: 2

Alessandro
Alessandro

Reputation: 3761

List<T> result = firstList.Except(secondList).ToList()

And you could need to implement an EqualityComparer<T> on your class to ensure equality on your objects.

Upvotes: 2

Related Questions