I'm trying to project from my Order
model to my OrderDTO
model. Order
has an enum. The problem is that projection doesn't work if I try to to get the Description attribute from the Enum. Here it's my code:
OrderStatus.cs:
public enum OrderStatus { [Description("Paid")] Paid, [Description("Processing")] InProcess, [Description("Delivered")] Sent }
Order.cs:
public class Order { public int Id { get; set; } public List<OrderLine> OrderLines { get; set; } public OrderStatus Status { get; set; } }
OrderDTO.cs:
public class OrderDTO { public int Id { get; set; } public List<OrderLineDTO> OrderLines { get; set; } public string Status { get; set; } }
With this following configuration in my AutoMapper.cs:
cfg.CreateMap<Order, OrderDTO>().ForMember(
dest => dest.Status,
opt => opt.MapFrom(src => src.Status.ToString())
);
Projection works, but I get an OrderDTO object like this:
- Id: 1
- OrderLines: List<OrderLines>
- Sent //I want "Delivered"!
I don't want Status
property to be "Sent", I want it to be as its associated Description attribute, in this case, "Delivered".
I have tried two solutions and none of them have worked:
ResolveUsing is not supported for projections, see the wiki on LINQ projections for supported operations.
Using a static method to return the Description attribute in String by Reflection.
cfg.CreateMap<Order, OrderDTO>().ForMember( dest => dest.Status, opt => opt.MapFrom(src => EnumHelper<OrderStatus>.GetEnumDescription(src.Status.ToString())) );
But this gives me the following error:
LINQ to Entities does not recognize the method 'System.String GetEnumDescription(System.String)' method, and this method cannot be translated into a store expression.
Then, how can I achieve this?