C# 如何在 AutoMapper 中配置条件映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17822882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:35:13  来源:igfitidea点击:

How to configure Conditional Mapping in AutoMapper?

c#lambdaautomapperautomapper-2

提问by Alwyn

Suppose I have the following entities (classes)

假设我有以下实体(类)

public class Target
{
    public string Value;
}


public class Source
{
    public string Value1;
    public string Value2;
}

Now I want to configure Auto Map, to Map Value1 to Value if Value1 starts with "A", but otherwise I want to map Value2 to Value.

现在我想配置自动映射,如果 Value1 以“A”开头,则将 Value1 映射到 Value,否则我想将 Value2 映射到 Value。

This is what I have so far:

这是我到目前为止:

Mapper
    .CreateMap<Source,Target>()
    .ForMember(t => t.Value, 
        o => 
            {
                o.Condition(s => 
                    s.Value1.StartsWith("A"));
                o.MapFrom(s => s.Value1);
                  <<***But then how do I supply the negative clause!?***>>
            })

However the part the still eludes me is how to tell AutoMapper to go take s.Value2should the earlier condition fails.

然而,我仍然无法理解的部分是如果早期条件失败,如何告诉 AutoMapper去执行s.Value2

It just seems to me the API was not designed as well as it could be... but may be it's my lack of knowledge getting in the way.

在我看来,API 的设计并没有达到应有的水平……但可能是我缺乏知识造成了障碍。

采纳答案by Tejal

Try this

尝试这个

 Mapper.CreateMap<Source, Target>()
        .ForMember(dest => dest.Value, 
                   opt => opt.MapFrom
                   (src => src.Value1.StartsWith("A") ? src.Value1 : src.Value2));

Condition option is used to add conditions to properties that must be met before that property will be mapped and MapFrom option is used to perform custom source/destination member mappings.

Condition 选项用于向在映射该属性之前必须满足的属性添加条件,而 MapFrom 选项用于执行自定义源/目标成员映射。

回答by Marcus.D

With the conditional mapping you only can configure when the mapping should executed for the specified destination property.

使用条件映射,您只能配置何时应为指定的目标属性执行映射。

So it means you can't define two mappings with different conditions for the same destination property.

所以这意味着你不能为同一个目标属性定义两个具有不同条件的映射。

If you have a condition like "if condition is true then use PropertyA else use PropertyB" then you should do it like "Tejal" wrote:

如果您有类似“如果条件为真,则使用 PropertyA,否则使用 PropertyB”之类的条件,那么您应该像“Tejal”写道的那样:

opt.MapFrom(src => src.Value1.StartsWith("A") ? src.Value1 : src.Value2)

回答by Khawaja Asim

AutoMapper allows you to add conditions to properties that must be met before that property will be mapped.

AutoMapper 允许您向在映射该属性之前必须满足的属性添加条件。

I was doing the mapping with some enum conditions, have a look that is little effort for the community from my side.

我正在使用一些枚举条件进行映射,看看我这边对社区来说是什么努力。

}

}

.ForMember(dest => dest.CurrentOrientationName, 
             opts => opts.MapFrom(src => src.IsLandscape? 
                                        PageSetupEditorOrientationViewModel.Orientation.Landscape : 
                                        PageSetupEditorOrientationViewModel.Orientation.Portrait));

回答by Basim

AutoMapper allows adding conditions to properties that must be met before that property will be mapped.

AutoMapper 允许向在映射该属性之前必须满足的属性添加条件。

Mapper.CreateMap<Source,Target>()
      .ForMember(t => t.Value, opt => 
            {
                opt.PreCondition(s => s.Value1.StartsWith("A"));
                opt.MapFrom(s => s.Value1);
            })