How to build a new dictionary with values from two different dictionaries having same keys

混江龙づ霸主 提交于 2019-12-23 03:14:43

问题


Let's say we have two dictionaries with the following key pair values:

  • Dictionary 1 : [ {1,abc} {2,cde} {3,efg} ]
  • Dictionary 2: [ {1,123} {2,234} {3,345} ]
  • I want to create a dictionary as follows: Dictionary 3 : [ {abc,123} {cde,234} {efg,345} ]

        fieldValues = new List<List<string>>();
        docFieldKey = new List<List<string>>();
        docFieldValueDict = new Dictionary<string, List<string>>();
        docKeyDict = new Dictionary<string, List<string>>();
        iterateDocKeyDict = new List<string>();
        iterateDocFldValueDict = new List<string>();
    
        //To read row wise and store key and all values
        for (limit = 0; limit < docNames.Count; limit++)
        {
            for (row = 0; row < excelData.Count; row++)
            {
                if (excelData[row] == docNames[limit])
                {
                    for (colLimit = row + 1; colLimit < fieldNames.Count; colLimit++)
                    {
                        flag = true;
                        fieldValues.Add(new List<string>());
                        fieldValues[limit].Add(excelData[colLimit]);
                    }
                    if (flag == true)
                    {
                        docFieldValueDict.Add(docNames[limit], fieldValues[limit]);                          
                    }
                }
            }
    
        }
    
        //To add concatenated key for each docName
        for (limit = 0; limit < docNames.Count; limit++)
        {
            for (colLimit = 0; colLimit < concatKey.Count; colLimit++)
            {
                if (concatKey[colLimit].Contains(docNames[limit]))
                {
                    for (col = colLimit; col < fieldNames.Count - 1; col++)
                    {
                        flag = true;
                        docFieldKey.Add(new List<string>());
                        docFieldKey[limit].Add(concatKey[col]);
                    }
                    if (flag == true)
                    {
                        docKeyDict.Add(docNames[limit], docFieldKey[limit]);
                    }
                }
            }
    
        }
    
        //to merge the key and the value from both dictionaries
        for (limit = 0; limit <docKeyDict.Count; limit++)
        {
            var docKeyDictCompare = docKeyDict.ElementAt(limit);
            var docFldValueDictCompare = docFieldValueDict.ElementAt(limit);
            iterateDocKeyDict = docKeyDictCompare.Value;
            iterateDocFldValueDict = docFldValueDictCompare.Value;
            if (docKeyDictCompare.Key == docFldValueDictCompare.Key)
            {
                for (col = 0; col < fieldNames.Count - 1; col++)
                {
                    if ((iterateDocKeyDict.ElementAt(col)!=string.Empty) && (iterateDocFldValueDict.ElementAt(col)!=string.Empty))
                    {
                        flag = true;
                    }
                }
    
                if (flag == true)
                {
                    keyValuePairDict.Add(iterateDocKeyDict.ElementAt(col), iterateDocFldValueDict.ElementAt(col));
                }
            }
        }
    

I tried the above but I get index out of range exception at

if ((iterateDocKeyDict.ElementAt(col)!=string.Empty) &&(iterateDocFldValueDict.ElementAt(col)!=string.Empty)) Error msg displayed in Console


回答1:


Just looking at your sample dictionaries I would suggest this:

var dic1 = /* [ {1,abc} {2,cde} {3,efg} ] */;
var dic2 = /* [ {1,123} {2,234} {3,345} ] */;
var dic3 = dic1.ToDictionary(x => x.Value, x => dic2[x.Key]);

But looking at your fairly long code sample I'm not sure it is enough.




回答2:


Try it like this:

var dictionaryC = dictionaryA.ToDictionary(dicA => dicA.Value, dicA => dictionaryB[dicA.Key]);


来源:https://stackoverflow.com/questions/39351583/how-to-build-a-new-dictionary-with-values-from-two-different-dictionaries-having

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!