简体   繁体   English

'将'Dictionary <string,int>转换为List <object>

[英]'Convert' Dictionary<string,int> into List<object>

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. 我有一个Dictionary<string,int> dictionary1 ,我需要将它转换为List<Data> ,其中Data具有属性lable = dictionary1.key和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. 我不想使用for / foreach循环(由我自己编写),因为为了避免它我试图使用Dictionary。

Another option would be having two different dictionaries (dictionary2 and dictionary3) where dictionary2<string,keyOfDictionary1> and dictionary3<string,valueOfDictionary1> . 另一种选择是拥有两个不同的词典(dictionary2和dictionary3),其中dictionary2<string,keyOfDictionary1>dictionary3<string,valueOfDictionary1>

Do I make sense? 我有道理吗? Is that possible? 那可能吗? Is there a better option? 有更好的选择吗?

Assuming: 假设:

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();

Perhaps you could use LINQ? 也许你可以使用LINQ?

dictionary1.Select(p => new Data(p.Key, p.Value)).ToList()

This is however using yield and thus loops in the background... 然而,这是使用yield ,因此在后台循环...

myDictionary.Select(x => new Data(){ label = x.Key, value = x.Value).ToList();

I assume that "no loop" actually means "i want LINQ": 我假设“无循环”实际上意味着“我想要LINQ”:

List<Data> = dictionary1.Select(
    pair => new Data() {
        label = pair.Key,
        value = pair.Value
    })).ToList();

尝试

dictionary1.Select(p => new Data(p.Key, p.Value)).ToList();

.NET already has a data type that does what Data would do: KeyValuePair<T1,T2> . .NET已经有一种数据类型可以执行Data操作: KeyValuePair<T1,T2> Dictionary already implements IEnumerable<KeyValuePair<T1,T2>> , just cast to it. Dictionary已经实现了IEnumerable<KeyValuePair<T1,T2>> ,只是强制转换它。

Dictionary<string, int> blah = new Dictionary<string, int>();
IEnumerable<KeyValuePair<string, int>> foo = blah;

This is a old post, but post only to help other persons ;) 这是一个老帖子,但帖子只是为了帮助其他人;)

Example to convert any object type: 转换任何对象类型的示例:

public List<T> Select<T>(string filterParam)
{    
    DataTable dataTable = new DataTable()

    //{... implement filter to fill dataTable }

    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row;

    foreach (DataRow dr in dataTable.Rows)
    {
        row = new Dictionary<string, object>();
        foreach (DataColumn col in dataTable.Columns)
        {
            row.Add(col.ColumnName, dr[col]);
        }
        rows.Add(row);
    }

    string json = new JavaScriptSerializer().Serialize(rows);

    using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
    {
        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T[]));
        var tick = (T[])deserializer.ReadObject(stream);
        return tick.ToList();
    }
}
    public class Data
    {
        public string Key { get; set; }

        public int Value { get; set; }
    }

    private static void Main(string[] args)
    {
        Dictionary<string, int> dictionary1 = new Dictionary<string, int>();
        dictionary1.Add("key1", 1);
        dictionary1.Add("key2", 2);

        List<Data> data = dictionary1.Select(z => new Data { Key = z.Key, Value = z.Value }).ToList();

        Console.ReadLine();
    }

Just in case just helps anyone, I did it like this - will handle objects more complex than a single value type, as stated by the OP. 以防万一只是帮助任何人,我这样做 - 将处理比单个值类型更复杂的对象,如OP所述。

// Assumes: Dictionary<string, MyObject> MyDictionary;
List<MyObject> list = new List<MyObject>();
list.AddRange(MyDictionary.Values.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM