// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class Data { public int Id { get; set; } public string Val { get; set; } public override bool Equals(object obj) { return (obj is Data) && (((Data)obj).Id == Id); } public override int GetHashCode() { return Id.GetHashCode(); } } public static class MyExtensions { public static List Merge1(this List oldlst, List newlst, Func kg) { Dictionary res = oldlst.ToDictionary(kg); newlst.ForEach(o => res[kg(o)] = o); return res.Values.ToList(); } public static List Merge2(this List oldlst, List newlst) { return newlst.Union(oldlst).ToList(); } public static List Merge3(this List oldlst, List newlst, Func kg) { oldlst.RemoveAll(x => newlst.Select(y => kg(y)).Contains(kg(x))); newlst.AddRange(oldlst.Where(y => !newlst.Select(u => kg(u)).Contains(kg(y)))); return newlst; } } public class Program { public static void TestExtension() { List oldlst = new List{ new Data { Id=1, Val="Old val 1" }, new Data { Id=2, Val="Old val 2" }, new Data { Id=3, Val="Old val 3" }}; Console.WriteLine("base:"); foreach(Data o in oldlst) { Console.WriteLine(o.Id + " : " + o.Val); } List newlst = new List{ new Data { Id=2, Val="New val 2" }, new Data { Id=4, Val="New val 4" }}; Console.WriteLine("updated:"); foreach(Data o in newlst) { Console.WriteLine(o.Id + " : " + o.Val); } List res1 = oldlst.Merge1(newlst, o => o.Id); Console.WriteLine("result:"); foreach(Data o in res1) { Console.WriteLine(o.Id + " : " + o.Val); } List res2 = oldlst.Merge2(newlst); Console.WriteLine("result:"); foreach(Data o in res2) { Console.WriteLine(o.Id + " : " + o.Val); } List res3 = oldlst.Merge3(newlst, o => o.Id); Console.WriteLine("result:"); foreach(Data o in newlst) { Console.WriteLine(o.Id + " : " + o.Val); } } public static void TestInline() { List oldlst = new List{ new Data { Id=1, Val="Old val 1" }, new Data { Id=2, Val="Old val 2" }, new Data { Id=3, Val="Old val 3" }}; Console.WriteLine("base:"); foreach(Data o in oldlst) { Console.WriteLine(o.Id + " : " + o.Val); } List newlst = new List{ new Data { Id=2, Val="New val 2" }, new Data { Id=4, Val="New val 4" }}; Console.WriteLine("updated:"); foreach(Data o in newlst) { Console.WriteLine(o.Id + " : " + o.Val); } Dictionary tmp = oldlst.ToDictionary(o => o.Id); newlst.ForEach(o => tmp[o.Id] = o); List res1 = tmp.Values.ToList(); Console.WriteLine("result:"); foreach(Data o in res1) { Console.WriteLine(o.Id + " : " + o.Val); } List res2 = newlst.Union(oldlst).ToList(); Console.WriteLine("result:"); foreach(Data o in res2) { Console.WriteLine(o.Id + " : " + o.Val); } oldlst.RemoveAll(x => newlst.Select(y => y.Id).Contains(x.Id)); newlst.AddRange(oldlst.Where(y => !newlst.Select(u => u.Id).Contains(y.Id))); Console.WriteLine("result:"); foreach(Data o in newlst) { Console.WriteLine(o.Id + " : " + o.Val); } } private const int N1 = 50000; private const int N2 = 50000; public static void TestPerformance() { List oldlst = new List(); for(int i = 0; i < N1; i++) { oldlst.Add(new Data { Id=(i+1), Val="Bla bla" }); } List newlst = new List(); for(int i = 0; i < N2; i++) { newlst.Add(new Data { Id=(N1/2+i+1), Val="Bla bla" }); } DateTime dt1bef = DateTime.Now; List res1 = oldlst.Merge1(newlst, o => o.Id); DateTime dt1aft = DateTime.Now; Console.WriteLine((dt1aft - dt1bef).TotalSeconds); DateTime dt2bef = DateTime.Now; List res2 = oldlst.Merge2(newlst); DateTime dt2aft = DateTime.Now; Console.WriteLine((dt2aft - dt2bef).TotalSeconds); DateTime dt3bef = DateTime.Now; List res3 = oldlst.Merge3(newlst, o => o.Id); DateTime dt3aft = DateTime.Now; Console.WriteLine((dt3aft - dt3bef).TotalSeconds); } public static void Main(string[] args) { TestExtension(); TestInline(); TestPerformance(); } } }