Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Diff two lists

5.00/5 (1 vote)
19 Dec 2010CPOL 5.2K  
public static class ListHelper{ public static List GetUpdatedList(this List oldList, List newList) where T : class { List itemsToAdd = new List(); List itemsToRemove = new List(oldList); foreach (var item in newList) { if...
public static class ListHelper
{
public static List<t> GetUpdatedList<t>(this List<t> oldList,
List<t> newList) where T : class
{
List<t> itemsToAdd = new List<t>();
List<t> itemsToRemove = new List<t>(oldList);
foreach (var item in newList)
{
if (oldList.Exists(t=&gt;t==item))
{
itemsToRemove.Remove(item);
}
else
{
itemsToAdd.Add(item);
}
}
return itemsToAdd.Concat(itemsToRemove).ToList();
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)