'转换'字典<字符串,整数>进入名单<对象> [英] 'Convert' Dictionary<string,int> into List<object>

查看:88
本文介绍了'转换'字典<字符串,整数>进入名单<对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个词典<字符串,整数> 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();

这篇关于'转换'字典&LT;字符串,整数&GT;进入名单&LT;对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆