0

I tried the following but it failed (as I expected it would) with "The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities."

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()));
Mohammad Akbari
  • 4,486
  • 6
  • 43
  • 74
  • Instead of having your custom Months array, you could use the builtin one. Check this question: http://stackoverflow.com/questions/3184121/get-month-name-from-month-number – vyrp Apr 05 '17 at 04:12
  • The `Map` method should work. From the exception looks like you are using `ProjectTo`. Please clarify your question. – Ivan Stoev Apr 05 '17 at 07:34
  • @Mohammed Akbari Please show me how you would use your suggestion in my original code. Thanks! – Chuck Glover Apr 06 '17 at 01:26

1 Answers1

0

If the date column is not null. You can refer my solution:

Mapper.CreateMap<DezReceiptModel, SelectablePaymentMonthYearViewModel>()
             .ForMember(dest => dest.DateOfServiceMonthCommaYear, opt => opt.MapFrom(src => src.DateOfService.ToString("MMM").ToUpper() + "-" + src.DateOfService.Year.ToString()));
Tomato32
  • 2,145
  • 1
  • 10
  • 10
  • This will not work because LINQ will not be able to translate .Tostring("MMM") to SQL. It is strange that it will translate .Tostring() but adding formatting will cause an exception. That was actually on of the first things I tried. – Chuck Glover Apr 06 '17 at 01:33