Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Diff two lists

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Dec 2010CPOL 5.1K   2  
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)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --