C# 使用AutoMapper创建日期时间
我正在努力从数据库返回的(年、月、日)创建DateTime对象。我对AutoMapper还比较陌生,所以朝着正确的方向轻推会很好 以下是包含DateTime对象和创建DateTime所需的三个值的ViewModel:C# 使用AutoMapper创建日期时间,c#,viewmodel,automapper,C#,Viewmodel,Automapper,我正在努力从数据库返回的(年、月、日)创建DateTime对象。我对AutoMapper还比较陌生,所以朝着正确的方向轻推会很好 以下是包含DateTime对象和创建DateTime所需的三个值的ViewModel: public class EnquiriesListViewModel { // other field elided public sbyte flightDay; public sbyte flightMonth; public bool fli
public class EnquiriesListViewModel
{
// other field elided
public sbyte flightDay;
public sbyte flightMonth;
public bool flightYear
public DateTime flightDate;
// other field elided
}
我希望AutoMapper根据其他三个值构造flightDate。我尝试过各种方法,其中一些甚至没有编译
像这样:
Mapper.CreateMap<enquiryListEntry, EnquiriesListViewModel>()
.ForMember(dest => dest.flightDate, /* what goes in here? */);
Mapper.CreateMap()
.ForMember(dest=>dest.flightDate,/*这里放什么?*/);
期待您的回复
MMapper.CreateMap()
.ForMember(dest=>dest.flightDate,opt.MapFrom(src=>newdatetime(src.flightYear,src.flightMonth,src.flightDay));
应该这样做。此解决方案来得太晚,但它很好,因为它适用于.NET 4.6.1
Mapper.CreateMap<enquiryListEntry, EnquiriesListViewModel>()
.ForMember(dest => dest.flightDate,
opt => opt.AddTransform(src => new DateTime(src.Year,
src.Month,
src.Day)));
Mapper.CreateMap()
.ForMember(dest=>dest.flightDate,
opt=>opt.AddTransform(src=>newdatetime(src.Year,
src.月,
src.Day),;
了解InquiryListentry的定义会很有帮助。@Richard,没错,我在提交答案时才意识到这一点。现在编辑它。
Mapper.CreateMap<enquiryListEntry, EnquiriesListViewModel>()
.ForMember(dest => dest.flightDate,
opt => opt.AddTransform(src => new DateTime(src.Year,
src.Month,
src.Day)));