使用Automapper覆盖现有映射

时间:2015-06-17 19:34:30

标签: c# linq enums automapper

我有以下实体&查看模型:

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&lt;&gt;

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));

2 个答案:

答案 0 :(得分:1)

使用Automapper 5.2.0版,您的语法应如下所示。您应该使用ResolveUsing而不是MapFrom,并且swith-case应该在大括号内。你不必使用任何Mapper.Initialize()。我测试了这段代码,它对我有用。我希望它对你也有帮助。欢呼声。

类似问题here。看看Mrchief的回答

   var config = new MapperConfiguration(cfg =>
                    {
        Mapper.CreateMap<Report, ReportViewModel>()
            .ForMember(src => src.Value, 
                       dest => dest.ResolveUsing(r => {
                            switch(reportingPeriod)
                            {
                                case ReportingPeriod.Daily:
                                    return r.Day.Value;
                                    break;
                                case ReportingPeriod.Weekly:
                                    return r.Week.Value;
                                    break;
                                case ReportingPeriod.Monthly:
                                    return r.Month.Value;
                                    break;
                                case default:
                                    //None
                                    return null;
                                    break;
                               }
                            }));
            });

       var mapper = config.CreateMapper();

答案 1 :(得分:0)

您需要返回该值。 IMO的switch语句比嵌套if语句更清晰:

Mapper.CreateMap<Report, ReportViewModel>()
    .ForMember(src => src.Value, 
               dest => dest.MapFrom(r => 
                    switch(reportingPeriod)
                    {
                        case ReportingPeriod.Daily:
                            return r.Day.Value;
                            break;
                        case ReportingPeriod.Weekly:
                            return r.Week.Value;
                            break;
                        case ReportingPeriod.Monthly:
                            return r.Month.Value;
                            break;
                        case default:
                            //None
                            return null;
                            break;
                    }));