如何为每个不同的键添加唯一编号?
最后我想要一个不同键的“集合”,但每个键也应该有一个值,例如current_index_of_collection + 1
elements.SelectMany(p => p.Properties.Keys).Distinct();
示例输出:
Key value
a 1
b 2
c 3
d 4
最佳答案
您是否在寻找 Select((value, index) => ...)
?
https://msdn.microsoft.com/en-us/library/bb534869(v=vs.110).aspx
var dictionary = elements
.SelectMany(p => p.Properties.Keys)
.Distinct()
.Select((key, index) => new {
key = key,
value = index + 1,
})
.ToDictionary(item => item.key, item => item.value);
或者
var array = elements
.SelectMany(p => p.Properties.Keys)
.Distinct()
.Select((key, index) => new KeyValuePair<MyKeyType, int>(key, index + 1))
.ToArray();
关于c# - 创建一个唯一的字典并添加自动增加的整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806274/