// 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 static class MyExtensions { public static List Merge(this List oldlst, List newlst, Func kg) { Dictionary res = oldlst.ToDictionary(kg); newlst.ForEach(o => res[kg(o)] = o); return res.Values.ToList(); } } public class Program { public static void Main(string[] args) { 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" }}; 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" }}; foreach(Data o in newlst) { Console.WriteLine(o.Id + " : " + o.Val); } List res = oldlst.Merge(newlst, o => o.Id); foreach(Data o in res) { Console.WriteLine(o.Id + " : " + o.Val); } } } }