LINQ - 如何使用Dictionary <string,string [] =“”>进行分组?

时间:2015-07-29 18:25:50

标签: c# arrays linq dictionary

我有这样的字典:

Dictionary<string, string[]> dic = new Dictionary<string,string[]>(){
            {"A", new string [] {"1", "2", "$", "3", "4"}},
            {"B", new string [] {"5", "6", "$", "7", "8"}},
            {"C", new string [] {"9", "10", "@", "11", "12"}}
        };

我想把它变成一个像这样的新词典:

Dictionary<string, List<string[]>> res = new Dictionary<string,List<string[]>>{
            {"$", new List<string[]> { 
                new string [] {"1", "2", "A", "3", "4"}, 
                new string [] {"5", "6", "B", "7", "8"} 
                }
            },               
            {"@", new List<string[]> { 
                new string [] {"9", "10", "C", "11", "12"}
                }
            }
        };

所以新的Key成为旧字符串数组的第3个元素,旧的Key被添加到新的字符串数组中。

注意 - 旧的Key不需要作为新数组的第3个元素放置,但它确实需要在每个新数组的相同索引中。

我开始尝试使用一些LINQ,但不能完全围绕整个事情 - 这个:

Dictionary<string, string[]> test = dic.GroupBy(x => x.Value[2])
.ToDictionary(s => s.Key, s => s.Select(x => x.Key).ToArray());

只能创建另一个字符串键控数组值字典,其中键正确地成为第3个元素,但值只是旧键。

2 个答案:

答案 0 :(得分:2)

一种可能的解决方案:

Dictionary<string, string[]> dic = new Dictionary<string,string[]>(){
            {"A", new string [] {"1", "2", "$", "3", "4"}},
            {"B", new string [] {"5", "6", "$", "7", "8"}},
            {"C", new string [] {"9", "10", "@", "11", "12"}}
        };

var res = dic.Select(p => new KeyValuePair<string, string[]>(
                              p.Value[2], 
                              p.Value.Select((v,i) => i == 2 ? p.Key : v).ToArray()))
             .GroupBy(p => p.Key)
             .ToDictionary(g => g.Key, g => g.Select(p => p.Value).ToList());

答案 1 :(得分:2)

var replacedIndex = 2;

var newDictionary = 
    oldDictionary.GroupBy(x => x.Value.ElementAt(replacedIndex))
                 .ToDictionary(group => group.Key, group => group.Select(x =>
                 {
                     var collection = x.Value;
                     collection[replacedIndex] = x.Key;
                     return collection;
                 }));