我有以下实体和视图模型:
public class Report
{
public int? Day { get; set; }
public int? Week { get; set; }
public int? Month { get; set; }
public int Year { get; set; }
}
public class ReportViewModel
{
public int Value { get; set; }
public int Year { get; set; }
}
为了实现这一点,我目前正在使用以下内容进行映射。
Mapper.Initialize(c =>
{
c.CreateMap<Report, ReportViewModel>();
}
然后使用`.Project.To<>`进行选择。
var models = Context.Project().To<ReportViewModel>().ToList();
我的问题是,我正在将枚举传递给这个查询,告诉我值是需要按月、按周还是按日。
public enum ReportingPeriod
{
None = 0,
Monthly = 1,
Weekly = 2,
Daily = 4
}
如何根据传递的枚举动态设置Value属性的映射?
我尝试在查询执行前用以下代码覆盖配置,但没有成功。
Mapper.CreateMap<Report, ReportViewModel>()
.ForMember(src => src.Value, dest => dest.MapFrom(r => (reportingPeriod == ReportingPeriod.Monthly) ?
r.Month.Value : reportingPeriod == ReportingPeriod.Weekly ?
r.Week.Value : r.Day.Value));