如何使用MVC Automapper从日期实体返回实际月份名称

时间:2017-04-05 03:41:03

标签: c# asp.net-mvc entity-framework linq automapper

我尝试了以下但是失败了(正如我预期的那样)“LINQ to Entities不支持LINQ表达式节点类型'ArrayIndex'。”

string[] Months = new string[] 
{
    "JAN",
    "FEB",
    "MAR",
    "APR",
    "MAY",
    "JUN",
    "JUL",
    "AUG",
    "SEP",
    "OCT",
    "NOV",
    "DEC",
};

Mapper.CreateMap<DezReceiptModel, SelectablePaymentMonthYearViewModel>()
     .ForMember(dest => dest.DateOfServiceMonthCommaYear, 
                opt => opt.MapFrom(src => Months[src.DateOfService.Month].ToString() + src.DateOfService.Year.ToString()));

1 个答案:

答案 0 :(得分:0)

如果日期列不为空。你可以参考我的解决方案:

Mapper.CreateMap<DezReceiptModel, SelectablePaymentMonthYearViewModel>()
             .ForMember(dest => dest.DateOfServiceMonthCommaYear, opt => opt.MapFrom(src => src.DateOfService.ToString("MMM").ToUpper() + "-" + src.DateOfService.Year.ToString()));