Get a Join on Two Dictionaries with different Datatypes in C#

后端 未结 2 907
走了就别回头了
走了就别回头了 2021-01-14 08:43

I have two dictionaries, namely

Dictionary DictA=new Dictionary();
Dictionary DictB=new Dictiona         


        
相关标签:
2条回答
  • 2021-01-14 09:14
    DictA.Join(DictB, 
               a => a.Key,
               b => b.Key,
               (a,b) => new KeyValuePair<int,string>(b.Value,a.Key))
        .ToDictionary(x => x.Key, x => x.Value);
    
    0 讨论(0)
  • 2021-01-14 09:20

    Very simple:

    var query =
        DictA
            .Where(a => DictB.ContainsKey(a.Key))
            .ToDictionary(a => DictB[a.Key], a => a.Value);
    

    I get this result:

    result

    0 讨论(0)
提交回复
热议问题