C# 使用 linq 将字典值转换为列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17925218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:51:00  来源:igfitidea点击:

Convert dictionary values to list using linq

c#linqlistdictionary

提问by Chris_web

Following code giving me 'Evaluation of lambda expressions is not valid in the debugger'. Please suggest where I am doing wrong from below -

下面的代码给我“lambda 表达式的评估在调试器中无效”。请从下面建议我做错的地方 -

List<MyFieldClass> lstFiedls;
lstFiedls = objDictionary.Select(item => item.Value).ToList();

Thanks,

谢谢,

采纳答案by Dustin Kingen

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:

您不需要使用 Linq 来获取值。在Dictionary(TKey, TValue)具有保存价值,属性Dictionary(TKey, TValue).Values

var fields = objDictionary.Values.ToList();

回答by Markus

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

仅尝试使用 ToList() 将字典的值转换为列表,您将收到编译器错误:

        Dictionary<int, int> dict = new Dictionary<int, int>();
        var result = dict.Values.ToList();

Unless you include "using System.Linq" in your file.

除非您在文件中包含“使用 System.Linq”。