score:1

Accepted answer

Now that this has been clarified, the question is basically this:

  • Combine all elements from list A and list B, and if there are any duplicates, keep the one with the newest LastGamePlayed.

I would do this with a grouping:

var players = from p in listA.Concat(listB)
              group p by p.ID into g
              select g.OrderByDescending(x => x.LastGamePlayed).First();

If performance is an issue, there are "faster" ways to write this, but I would start with this.

score:2

You should be able to use the linq enumerable extension Concat and Exept to achieve this.

listA.Concat(listB.Except(listA);

This will remove items in B that match A, and them add the result to A.

You will have to write an IEqualityComparer which compares by ID.

Documentation for theses methods can be found here:

MSDN Enumerable Extensions

IEqualityComparer documentation can be found here:

MSDN IEqualityComparer

score:0

You can write an O(n log n) solution by building a set from list2 then overwriting (or inserting) items with list1, then converting that set back to a list.

If the lists are already sorted, you could do it O(n) by merging by hand.


Related Articles