How to map an int to its enum description using AutoMapper during a queryable projection?

automapper automapper-4 entity-framework-6 enums iqueryable


Here is the enum extension method to get its description attribute.

public static string GetDescription(this Enum enumeration)
    if (enumeration == null)
        throw new ArgumentNullException();

    var value = enumeration.ToString();
    var type = enumeration.GetType();
    var descriptionAttribute =
        (DescriptionAttribute[]) type.GetField(value).GetCustomAttributes(typeof (DescriptionAttribute), false);

    return descriptionAttribute.Length > 0 ? descriptionAttribute[0].Description : value;

Here is the source object:

public class Account {
    public int AccountId {get;set;}
    public int AccountStatusId {get;set;}

Here is the enum:

public enum AccountStatus {
    [Description("BAD CREDIT")

Here is the destination object:

public class GetAccountResponse {
    public int AccountId {get;set;}
    public string Status {get;set;}

Here is my attempt to map (using the latest non-static automapper version). Remember this is during an EF queryable projection.

_config = new MapperConfiguration(cfg => cfg.CreateMap<Account, GetAccountsResponse>()
    .ForMember(dest => dest.Status,
        opts => opts.MapFrom(src => ((AccountStatus) src.AccountStatusId).GetDescription())));

Here is the projection where query is an IQueryable<Account>:


This is the exception I get:

Can't resolve this to Queryable Expression

3/31/2016 12:08:12 PM

Accepted Answer

If you check out the signature of the MapFrom method, you'll notice that one of the overloads takes a parameter of type Expression<Func<TSource, TMember>>.

This suggests that you could write a method which builds an expression tree from ternary expressions that can convert any possible value of your enum to its appropriate string. AutoMapper would then convert this into the appropriate SQL expression via LINQ.

Here's an example which just uses the Enum names themselves: you should be able to adapt it straightforwardly to use your Descriptions:

public static class EnumerableExpressionHelper
    public static Expression<Func<TSource, String>> CreateEnumToStringExpression<TSource, TMember>(
        Expression<Func<TSource, TMember>> memberAccess, string defaultValue = "")
        var type = typeof(TMember);
        if (!type.IsEnum)
            throw new InvalidOperationException("TMember must be an Enum type");

        var enumNames = Enum.GetNames(type);
        var enumValues = (TMember[])Enum.GetValues(type);

        var inner = (Expression)Expression.Constant(defaultValue);

        var parameter = memberAccess.Parameters[0];

        for (int i = 0; i < enumValues.Length; i++)
            inner = Expression.Condition(
            Expression.Equal(memberAccess.Body, Expression.Constant(enumValues[i])),

        var expression = Expression.Lambda<Func<TSource,String>>(inner, parameter);

        return expression;

You would use it as follows:

CreateMap<Entry, EntryListItem>()
            .ForMember(e => e.ReviewStatus,
                c => c.MapFrom(EnumerableExpressionHelper.CreateEnumToStringExpression((Entry e) => e.ReviewStatus)))
7/5/2016 4:02:03 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow