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)
按值对字典进行分组,并将组键映射到键列表:
input.GroupBy(x => x.Value).ToDictionary(x => x.Key, x => x.Select(_ => _.Key).ToList())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
175 次 |
| 最近记录: |