加入4个字典

时间:2019-07-18 09:46:05

标签: c# dictionary

我有4个词典,我需要通过组合/比较所有键来显示这些词典

在所有4个词典中,键值可以相同或不同,但是值不同。

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

3 个答案:

答案 0 :(得分:3)

加入(键来自dictionary1)。主要原理是

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, 
   });

在您的情况下(intdouble值),我们必须以某种方式标记 abscent 值;让我们为此使用 nullable int?double?

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 
   });

编辑:如果要完全加入,而不是一个,即 all的 all 个键字典出现,让我们得到它们:

var keys = dictionary1.Keys
  .Union(dictionary2.Keys)
  .Union(dictionary3.Keys)
  .Union(dictionary4.Keys);  

然后再次:

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
   }) 

答案 1 :(得分:1)

尝试以下操作:

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
                });
            }
        }


    }


}

答案 2 :(得分:1)

将字典放入数组中,选择不同的键,为每个键选择值:

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();
    }
}

enter image description here