AutoMapper and convert a datetime to string
try this:
Mapper.CreateMap<I_NEWS, NewsModel>().ForMember(x => x.DateCreated,
opt => opt.MapFrom(src => ((DateTime)src.DateCreated).ToShortDateString()));
In order to use lambda bodies, use .ResolveUsing
instead of .MapFrom
.
As per the author:
MapFrom has some extra stuff that needs expression trees (like null checking etc).
So your statement would look like this:
Mapper.CreateMap<I_NEWS, NewsModel>()
.ForMember(x => x.DateCreated, opt => opt.ResolveUsing(src => {
var dt = (DateTime)src.DateCreated;
return dt.ToShortDateString();
}));