Following code giving me 'Evaluation of lambda expressions is not valid in the debugger'. Please suggest where I am doing wrong from below -
List<MyFieldClass> lstFiedls;
lstFiedls = objDictionary.Select(item => item.Value).ToList();
Thanks,
Following code giving me 'Evaluation of lambda expressions is not valid in the debugger'. Please suggest where I am doing wrong from below -
List<MyFieldClass> lstFiedls;
lstFiedls = objDictionary.Select(item => item.Value).ToList();
Thanks,
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();
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.
With C# 7 it is possible to use tuples, which makes this conversion much simpler and you can have 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