Reputation: 23
I have 4 dictionaries, I need to display those dictionaries with combining/comparing all keys
In all 4 dictionaries, the key values may be same or different but the values are different.
dict1<String,int>-(aa,1)(bb,2)
dict2<String,double>(bb,2)
dict3<String,int>-(aa,0)
dict4<String,double>-(aa,5)(bb,7)
key1 value1 value2 value3 value4
aa 1 - 0 5
bb 2 2 - 7
Upvotes: 1
Views: 93
Reputation: 186708
Left join (keys are from dictionary1
). The main principle is
var result = dictionary1
.Select(pair => new {
key1 = pair.Key,
value1 = pair.Value,
// you may want to put some default value(s) instead of null
value2 = dictionary2.TryGetValue(pair.Key, out var v2) ? v2 : null,
value3 = dictionary3.TryGetValue(pair.Key, out var v3) ? v3 : null,
value4 = dictionary4.TryGetValue(pair.Key, out var v4) ? v4 : null,
...
valueN = dictionaryN.TryGetValue(pair.Key, out var vN) ? vN : null,
});
in your case (int
and double
values) we have to mark abscent values somehow; let's use nullable int?
and double?
for this.
var result = dictionary1
.ToDictionary(pair => pair.Key,
pair => new {
value1 = pair.Value,
value2 = dictionary2.TryGetValue(pair.Key, out var v2) ? (double?)v2 : null,
value3 = dictionary3.TryGetValue(pair.Key, out var v3) ? (int?)v3 : null,
value4 = dictionary4.TryGetValue(pair.Key, out var v4) ? (double?)v4 : null
});
Edit: If you want Full join, not Left one i.e. all keys of all dictionaries be present, let's get them:
var keys = dictionary1.Keys
.Union(dictionary2.Keys)
.Union(dictionary3.Keys)
.Union(dictionary4.Keys);
and then again:
var result = keys
.ToDictionary(key => key,
key => new {
value2 = dictionary1.TryGetValue(key, out var v1) ? (int?)v1 : null,
value2 = dictionary2.TryGetValue(key, out var v2) ? (double?)v2 : null,
value3 = dictionary3.TryGetValue(key, out var v3) ? (int?)v3 : null,
value4 = dictionary4.TryGetValue(key, out var v4) ? (double?)v4 : null
})
Upvotes: 3
Reputation: 1802
Put dictionaries in array, select distinct keys, select values for each key:
class Program
{
static void Main()
{
var dicts = new IDictionary[] {
new Dictionary<string, int>() { { "aa", 1 }, { "bb", 2 } },
new Dictionary<string, double>() { { "bb", 2.0 } },
new Dictionary<string, int>() { { "aa", 0 } },
new Dictionary<string, double>() { { "aa", 5.0 }, { "bb", 7.0 } },
};
var keys = dicts.SelectMany(d => d.Keys as IEnumerable<object>).Distinct();
foreach (var p in keys.Select(k => (k, vals: dicts.Select(d => d[k]))))
{
Console.WriteLine(p.k + "\t" + string.Join("\t", p.vals));
}
Console.ReadLine();
}
}
Upvotes: 1
Reputation: 34421
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication120
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, decimal> dict1 = new Dictionary<string, decimal>() { { "aa", 1 }, { "bb", 2 } };
Dictionary<string, decimal> dict2 = new Dictionary<string, decimal>() { { "bb", 2 } };
Dictionary<string, decimal> dict3 = new Dictionary<string, decimal>() { { "aa", 0 } };
Dictionary<string, decimal> dict4 = new Dictionary<string, decimal>() { { "aa", 5 }, { "bb", 7 } };
List<string> keys = dict1.Keys.ToList();
keys.AddRange(dict2.Keys);
keys.AddRange(dict3.Keys);
keys.AddRange(dict4.Keys);
keys = keys.Distinct().OrderBy(x => x).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Dict 1", typeof(decimal));
dt.Columns.Add("Dict 2", typeof(decimal));
dt.Columns.Add("Dict 3", typeof(decimal));
dt.Columns.Add("Dict 4", typeof(decimal));
decimal value1 = 0;
decimal value2 = 0;
decimal value3 = 0;
decimal value4 = 0;
foreach (string key in keys)
{
Boolean foundValue1 = dict1.TryGetValue(key, out value1);
Boolean foundValue2 = dict2.TryGetValue(key, out value2);
Boolean foundValue3 = dict3.TryGetValue(key, out value3);
Boolean foundValue4 = dict4.TryGetValue(key, out value4);
dt.Rows.Add(new object[] {
key,
foundValue1 ? (decimal?)value1 : null,
foundValue2 ? (decimal?)value2 : null,
foundValue3 ? (decimal?)value3 : null,
foundValue4 ? (decimal?)value4 : null
});
}
}
}
}
Upvotes: 1