9

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?


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

4

9 に答える 9

19

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();
于 2012-07-13T12:17:59.320 に答える
5

おそらくLINQを使用できますか?

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

ただし、これは使用yieldしているため、バックグラウンドでループします...

于 2012-07-13T12:17:22.883 に答える
4
myDictionary.Select(x => new Data(){ label = x.Key, value = x.Value).ToList();
于 2012-07-13T12:17:32.863 に答える
2

I assume that "no loop" actually means "i want LINQ":

List<Data> = dictionary1.Select(
    pair => new Data() {
        label = pair.Key,
        value = pair.Value
    })).ToList();
于 2012-07-13T12:19:21.023 に答える
1

Try

dictionary1.Select(p => new Data(p.Key, p.Value)).ToList();
于 2012-07-13T12:18:16.043 に答える
1

.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;
于 2012-07-13T12:21:48.887 に答える
1

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();
    }
}
于 2015-07-14T14:27:51.847 に答える
0
    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();
    }
于 2012-07-13T12:17:54.337 に答える
0

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());
于 2017-06-21T23:48:47.647 に答える