我有一个字典,我想将其转换为另一个字典,使用以下示例隐含的规则:
输入:
Dictionary<string, string> inputs = new Dictionary<string, string>(3)
{
{ "A", "First" },
{ "Z", "Third" },
{ "J", "Second" }
};
Run Code Online (Sandbox Code Playgroud)
输出:
Dictionary<int, string> output = new Dictionary<string, string>(3)
{
{ 0, "First" },
{ 1, "Second" },
{ 2, "Third" }
};
Run Code Online (Sandbox Code Playgroud)
我可以使用lambda语法和没有中间对象吗?
谢谢.
字典的枚举顺序是不确定的(即项目没有索引),所以我不确定这是否可行.如何导出整数值?
编辑:
我现在明白了:
inputs
.OrderBy(input => input.Key)
.Select((input, index) => new {index, input.Value})
.ToDictionary(x => x.index, x => x.Value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5562 次 |
| 最近记录: |