Let’s say I have a class,
Class A
{
ID;
Name;
LastModifiedDate;
}
and I have Two List’s, ListA1 and ListA2,
ListA1 = new List<A>();
ListA2 = new List<A>();
ListA1.Add(new A{ ID=1, Name="A", LastModifiedDate='1/1/2000' })
ListA1.Add(new A{ ID=2, Name="B", LastModifiedDate='1/2/2000' })
ListA1.Add(new A{ ID=3,Name="C", LastModifiedDate='1/2/2000' })
...................................................
ListA2.Add(new A{ ID=4, Name="D", LastModifiedDate='1/4/2000' })
ListA2.Add(new A{ ID=1, Name="A", LastModifiedDate='1/5/2000' })
ListA3.Add(new A{ ID=2, Name="B", LastModifiedDate='1/2/2000' })
Now, I need to compare ListA1 and ListA2 by comparing LastModifiedDate. I need two new List’s, describing what are new/updated items in ListA1 and describing what are new/updated items in ListA2.
Update: The output I ma looking is that,
newUpadtedListA ={
new A{ ID=4, Name="D", LastModifiedDate='1/4/2000' },
new A{ ID=1, Name="A", LastModifiedDate='1/5/2000' }
}
newUpadtedListB ={
new A{ ID=3, Name="C", LastModifiedDate='1/2/2000' }
}
You can use Linq and
Enumerable.Except:and the opposite, what’s new in
List2:Edit: according to your last edit that
IDis the only key column. This should give you all you need in a fairly efficient way:You can enumerate these sequences and do whatever you need to do to add or update the new/changed items.