从整数返回枚举名称在LINQ数据库查询 [英] Return Enum Name from Integer in LINQ Database Query

查看:362
本文介绍了从整数返回枚举名称在LINQ数据库查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回存储在使用LINQ查询数据库中的一个整数枚举的字符串值。

I would like to return the string value of an enum stored as an integer in a database using a LINQ query.

我曾尝试:

  return (from a in context.Tasks
                    select new TaskSearch
                    {
                        TaskID = a.TaskID,
                        TaskTypeName = Enum.GetName(typeof(TaskTypeEnum), a.TaskType)
                    }).ToList();

我使用asp.net的MVC。

I'm using asp.net mvc.

例外
System.NotSupportedException类型的异常出现在EntityFramework.SqlServer.dll但在用户code没有处理

Exception: An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

更多信息:LINQ到实体无​​法识别方法'System.String的GetName(的System.Type,System.Object的)的方法,而这种方法不能被翻译成店前pression

Additional information: LINQ to Entities does not recognize the method 'System.String GetName(System.Type, System.Object)' method, and this method cannot be translated into a store expression.

推荐答案

您需要兑现您的查询(查询必须能够被转换成SQL语句,但 Enum.GetName()不能转换为SQL)

You need to materialize your query (a query must be able to be converted to a sql statement, but Enum.GetName() cannot be converted to sql)

尝试

((from a in context.Tasks select a).AsEnumerable().Select(t => new TaskSearch
{
  TaskID = t.TaskID,
  TaskTypeName = Enum.GetName(typeof(TaskTypeEnum), t.TaskType)

}).ToList());

这篇关于从整数返回枚举名称在LINQ数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆