score:78

Accepted answer

you don't need to use linq to get the values. the dictionary(tkey, tvalue) has a property that holds the values, dictionary(tkey, tvalue).values:

var fields = objdictionary.values.tolist();

score:1

with c# 7 it is possible to use tuples, which makes this conversion much simpler and you can have the both the key and the value in the list.

   var mylist = new list<(tkey, tvalue)>(dic.select(x => (tkey, tvalue)));

full example

       dictionary<int, string> dic = new dictionary<int, string>();
        dic.add(1, "test1");
        dic.add(2, "test2");
        dic.add(3, "test3");

        
        var mylist = new list<(int, string)>(dic.select(x => (x.key, x.value)));

        foreach (var myitem in mylist)
        {
            console.writeline($"int val = {myitem.item1} string = {myitem.item2}");
        }

result

int val = 1 string = test1
int val = 2 string = test2
int val = 3 string = test3

score:5

you will get a compiler error simply trying to convert a dictionary's values to a list with tolist():

        dictionary<int, int> dict = new dictionary<int, int>();
        var result = dict.values.tolist();

unless you include "using system.linq" in your file.


Related Query

More Query from same tag