将Dictionary <string,int>转换为Dictionary <int,List <string >>

par*_*ant 2 c# linq dictionary

:我怎样才能最有效地转换Dictionary<string, int> to a Dictionary<int, List<string>>

var input = new Dictionary<string, int>() { {"A", 1}, {"B", 1}, {"C", 2} ...
Dictionary<int, List<string>> result = Transform(input)
Assert.IsTrue(result, { {1, {"A", "B"}}, {2, {"C"}} ... });
Run Code Online (Sandbox Code Playgroud)

Sel*_*enç 5

按值对字典进行分组,并将组键映射到键列表:

input.GroupBy(x => x.Value).ToDictionary(x => x.Key, x => x.Select(_ => _.Key).ToList())
Run Code Online (Sandbox Code Playgroud)