简体   繁体   中英

'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. 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.

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?

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?

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

This is however using yield and thus loops in the background...

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

I assume that "no loop" actually means "i want 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> . Dictionary already implements IEnumerable<KeyValuePair<T1,T2>> , just cast to it.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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