1

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

What I have tried:

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

I'm using asp.net mvc.

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

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.

1
  • Are you getting an exception? Commented Oct 30, 2014 at 4:47

3 Answers 3

4

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)

Try

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

}).ToList());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm new to this stuff, the code does not seem to compile - A query body must end with a select clause or a group clause. return ((from a in context.Tasks).AsEnumerable().Select(t => new TaskSearch { TaskID = t.TaskID, TaskTypeName = Enum.GetName(typeof(TaskTypeEnum), t.TaskType) } ).ToList();
0

I define a set of Enum extensions like this which I have found useful. Unfortunately the Type constraints cannot be refined beyond struct, so you must externally ensure that the methods are only called on 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
}

1 Comment

I may misunderstand but I don't see how your solution helps. I believe the problem lies with LINQ not recognizing the enum.parse method.
-1

Your error shows that query is unable to understand the Enum.GetName() method. You can cater this problem by just typeCasting your integer value in the query.

Try

((from a in context.Tasks select a).AsEnumerable().Select(t => new 
    TaskSearch
    {
       TaskID = t.TaskID,
       TaskTypeName = (TaskTypeEnum)t.TaskType
}).ToList());

1 Comment

That does not "return the string value of an enum" as the question states. In fact, the cast does nothing because t.TaskType already is an enum value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.