[Solved] Merging arrays of dictionaries


How about the following:

//define data1 and Data2
var data1 = new[]{
                   new {id = 1, name = "Oneeee", version = 2},
                   new {id = 2, name = "Two", version = 1},
                   new {id = 3, name = "Three", version = 2},
                   new {id = 4, name = "Four", version = 1},
                   new {id = 5, name ="Five", version = 1}
                 };

var data2 = new[] {
                   new {id = 1,  name = "One", version = 1},
                   new {id = 2, name = "Two", version = 1},
                   new {id = 3, name = "Threeee", version = 3},
                   new {id = 6, name = "Six", version = 2}
                 };
//create a dictionary to handle lookups     
var dict1 = data1.ToDictionary (k => k.id);
var dict2 = data2.ToDictionary (k => k.id);

// now query the data
var q = from k in dict1.Keys.Union(dict2.Keys)
        select
                dict1.ContainsKey(k) ? 
                  (
                    dict2.ContainsKey(k) ?
                    (
                      dict1[k].version > dict2[k].version ? dict1[k] : dict2[k]
                    ) :
                    dict1[k]
                  ) :
                  dict2[k];

// convert enumerable back to array
var result = q.ToArray();

Alternative solution that is database friendly if data1 and data2 are tables….

 var q  = (
            from d1 in data1
            join d2 in data2 on d1.id equals d2.id into data2j
            from d2j in data2j.DefaultIfEmpty()
            where d2j == null || d1.version >= d2j.version
            select d1
          ).Union(
            from d2 in data2
            join d1 in data1 on d2.id equals d1.id into data1j
            from d1j in data1j.DefaultIfEmpty()
           where d1j == null || d2.version > d1j.version
           select d2
          );

  var result = q.ToArray();

solved Merging arrays of dictionaries