'转换'字典<字符串,整数>进入名单<对象> [英] 'Convert' Dictionary<string,int> into List<object>
问题描述
我有一个词典<字符串,整数> dictionary1
,我需要将其转换成列表<数据>
,其中数据
的性能拉布勒
= dictionary1.key和值
= dictionary1.value。我不想使用for / foreach循环(我自己写的),因为为了避免它,我想使用的字典。
I have a Dictionary<string,int> dictionary1
and I need to convert it into a List<Data>
where Data
has the properties lable
= dictionary1.key and value
= dictionary1.value. I don't want to use a for/foreach loop (written by myself) because in order to avoid it I am trying to use a Dictionary.
另一个选择是有两个不同的字典(dictionary2和dictionary3),其中 dictionary2<字符串,keyOfDictionary1>
和 dictionary3<字符串,valueOfDictionary1>
。
Another option would be having two different dictionaries (dictionary2 and dictionary3) where dictionary2<string,keyOfDictionary1>
and dictionary3<string,valueOfDictionary1>
.
我有意义吗?那可能吗? ?有没有更好的选择。
Do I make sense? Is that possible? Is there a better option?
推荐答案
假设:
class Data
{
public string Label { get; set; }
public int Value { get; set; }
}
然后:
Then:
Dictionary<string, int> dic;
List<Data> list = dic.Select(p => new Data { Label = p.Key, Value = p.Value }).ToList();
这篇关于'转换'字典<字符串,整数>进入名单<对象>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!