Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C字典LINQ_Linq_List_Dictionary - Fatal编程技术网

C字典LINQ

C字典LINQ,linq,list,dictionary,Linq,List,Dictionary,我有一个类,它返回下面数据库表代码中的所有项。我还有一个包含键、值对的字典。在从函数返回数据之前,是否可以查看字典并从linq返回与键关联的值 public IEnumerable<Code> Return_All_Codes() { return _db.Codes.ToList(); //can the above list look inside my dictionary below and return Admi

我有一个类,它返回下面数据库表代码中的所有项。我还有一个包含键、值对的字典。在从函数返回数据之前,是否可以查看字典并从linq返回与键关联的值

public IEnumerable<Code> Return_All_Codes()
        {
           return _db.Codes.ToList(); 
           //can the above list look inside my dictionary below and return Administrative when it find the number 1 for my code type?  


        }


 public Dictionary<int, string> GetCodeTypes()
        {

            var dict = new Dictionary<int, string>();
            dict.Add(1, "Administrative");
            dict.Add(2, "Regular");

            return dict; 
        }

当上面的列表找到我的代码类型的数字1时,是否可以查看我的字典并返回Administrative?在我的LINQ中有一种IF语句?如果1,则为管理,否则为常规

您可以通过以下方式进行映射:

var dict = GetCodeTypes();
return _db.Codes.AsEnumerable().Select(code => dict[code.cType]).ToList();

但是,我建议将字典存储在类中,以防止每次创建字典,特别是在多次调用字典的情况下。

您可以通过以下方式进行映射:

var dict = GetCodeTypes();
return _db.Codes.AsEnumerable().Select(code => dict[code.cType]).ToList();

但是,我建议将字典存储在类中,以防止每次创建字典,特别是在多次调用字典的情况下。

表代码大约有5个字段,其中一个是cType。cType是一个int,我想显示字典中的值。上面的代码允许我这样做吗?@MikeB55上面的代码现在将返回一个包含字典中相关字符串的列表。如果您需要其他信息,只需更改选择以包含它……当我使用上述代码时,我遇到了一个错误。错误:“System.Collections.Generic.Dictionary.this[int]”的最佳重载方法匹配有一些无效的arguments@MikeB55cType不能是整型。。检查一下,确认一下?可能是uint吗?我的cType的类型是Int32。我还尝试了以下代码:return _db.Codes.AsEnumerable.Selectcode=>dict[Convert.ToInt32code.cType].ToList;我将code.cType转换为Int32,但仍然得到一个错误。错误:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。表代码大约有5个字段,其中一个是cType。cType是一个int,我想显示字典中的值。上面的代码允许我这样做吗?@MikeB55上面的代码现在将返回一个包含字典中相关字符串的列表。如果您需要其他信息,只需更改选择以包含它……当我使用上述代码时,我遇到了一个错误。错误:“System.Collections.Generic.Dictionary.this[int]”的最佳重载方法匹配有一些无效的arguments@MikeB55cType不能是整型。。检查一下,确认一下?可能是uint吗?我的cType的类型是Int32。我还尝试了以下代码:return _db.Codes.AsEnumerable.Selectcode=>dict[Convert.ToInt32code.cType].ToList;我将code.cType转换为Int32,但仍然得到一个错误。错误:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”