【问题标题】:Return Enum Name from Integer in LINQ Database Query从 LINQ 数据库查询中的整数返回枚举名称
【发布时间】:2014-10-30 04:40:35
【问题描述】:

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

我尝试过的:

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

我正在使用 asp.net mvc。

异常: EntityFramework.SqlServer.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理

附加信息:LINQ to Entities 无法识别方法 'System.String GetName(System.Type, System.Object)' 方法,并且此方法无法转换为存储表达式。

【问题讨论】:

  • 你遇到异常了吗?
  • @Stephen 我已经编辑了我的问题,谢谢。

标签: c# asp.net-mvc linq


【解决方案1】:

您需要具体化您的查询(查询必须能够转换为sql语句,但Enum.GetName()不能转换为sql)

试试

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

}).ToList());

【讨论】:

  • 谢谢,我是新手,代码似乎无法编译 - 查询主体必须以 select 子句或 group 子句结尾。 return ((from a in context.Tasks).AsEnumerable().Select(t => new TaskSearch { TaskID = t.TaskID, TaskTypeName = Enum.GetName(typeof(TaskTypeEnum), t.TaskType) } ).ToList();
  • 太棒了,非常感谢。
【解决方案2】:

我定义了一组这样的枚举扩展,我发现它们很有用。不幸的是,Type 约束无法在struct 之外进行细化,因此您必须在外部确保这些方法仅在 Enums 上调用。:

/// <summary>Type-safe extension methods for parsing Enums.</summary>
public static partial class EnumExtensions{
  #region Enum Parsing utilities
  /// <summary>Typesafe wrapper for <c>Enum.GetValues(typeof(TEnum).</c></summary>
  public static ReadOnlyCollection<TEnum> EnumGetValues<TEnum>() {
    return new ReadOnlyCollection<TEnum>((TEnum[])(Enum.GetValues(typeof(TEnum))));
  }

  /// <summary>TODO</summary>
 [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
  public static ReadOnlyCollection<string> EnumGetNames<TEnum>() where TEnum : struct {
    return new ReadOnlyCollection<string>((string[])(Enum.GetNames(typeof(TEnum))));
  }

  /// <summary>Typesafe wrapper for <c>Enum.ParseEnum()</c> that automatically checks 
  /// constants for membership in the <c>enum</c>.</summary>
  public static TEnum ParseEnum<TEnum>(string value) where TEnum : struct {
    return ParseEnum<TEnum>(value,true);
  }

  /// <summary>Typesafe wrapper for <c>Enum.ParseEnum()</c> that automatically checks 
  /// constants for membership in the <c>enum</c>.</summary>
  public static TEnum ParseEnum<TEnum>(string value, bool checkConstants) where TEnum : struct {
    TEnum enumValue;
    if (!TryParseEnum<TEnum>(value, out enumValue) && checkConstants) 
          throw new ArgumentOutOfRangeException("value",value,"Enum type: " + typeof(TEnum).Name);

    return enumValue;
  }

  /// <summary>Typesafe wrapper for <c>Enum.TryParseEnum()</c> that automatically checks 
  /// constants for membership in the <c>enum</c>.</summary>
  public static bool TryParseEnum<TEnum>(string value, out TEnum enumValue) where TEnum : struct {
    return Enum.TryParse<TEnum>(value, out enumValue)  
       &&  Enum.IsDefined(typeof(TEnum),enumValue);
  }

  /// <summary>Typesafe wrapper for <c>Enum.ToObject()</c>.</summary>
  /// <typeparam name="TEnum"></typeparam>
  public static TEnum EnumParse<TEnum>(char c, string lookup) {
    if (lookup==null) throw new ArgumentNullException("lookup");
    var index = lookup.IndexOf(c);
    if (index == -1) throw new ArgumentOutOfRangeException("c",c,"Enum Type: " + typeof(TEnum).Name);

    return (TEnum) Enum.ToObject(typeof(TEnum), index);
  }
  #endregion
}

【讨论】:

  • 我可能会误解,但我看不出您的解决方案有何帮助。我认为问题在于 LINQ 无法识别 enum.parse 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多