Search code examples
c#asp.netlistenumerableicomparer

SortedList Compare


I have two sortedlists

 1. oldlist<int,int> 

 2. newlist <int,int>

(application specific information - key is industryId and value is weight)

I want to compare the changes in the lists.

I want the following things -

  • list of items where weight was not zero, but its zero in the newlist.

  • list of items where weight is not zero and has changed from oldlist.

I know there is something called as comparer. can it be used here ?


Solution

  • You can use Linq:

    // list of items where weight was not zero, but its zero in the newlist.
    var result1 = from o in oldList
                  join n in newList on o.Key equals n.Key 
                  where o.Value != 0 && n.Value == 0
                  select new {Old = o, New = n};
    
    // list of items where weight is not zero and has changed from oldlist.
    var result2 = from o in oldList
                  join n in newList on o.Key equals n.Key
                  where o.Value != 0 && o.Value != n.Value
                  select new { Old = o, New = n };