简体   繁体   English

使用Automapper进行自定义映射,其中目标中的字段是源中两个字段的串联

[英]Custom mapping with Automapper where a field in destination is the concatenation of two fields in source

I think that the title already quite explains the problem. 我认为这个标题已经很好地解释了这个问题。 I have a source type: 我有一个源类型:

public class Employee
{
    public string Name { get; set; }
    public string DateOfBirth { get; set; }
    public string srcImage { get; set; }
    public string Email { get; set; }
    public string Role { get; set; }
}

and

public class EmployeeViewModel
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string DateOfBirth { get; set; }
    public string Email { get; set; }
    public string Role { get; set; }
}

I want to use automapper to convert from EmployeeViewModel to Employee and the name of Employee is the concatenation of name and surname in EmployeeViewModel. 我想使用automapper从EmployeeViewModel转换为Employee,Employee的名称是EmployeeViewModel中名称姓氏的串联。

May you kindly explain me how to set the MapperConfiguration? 请您解释一下如何设置MapperConfiguration? Thanks! 谢谢!

Try this: 尝试这个:

Mapper.CreateMap<EmployeeViewModel, Employee>()
                        .ForMember(d => d.Name, d => d.MapFrom(x => string.Format("{0}{1}", x.Name, x.Surname)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动映射:在映射中一起使用源和目标 - Automapper: use source and destination together in mapping 基于源和目标的 AutoMapper 条件映射 - AutoMapper Conditional Mapping based on source and destination 如何获取映射的源成员和目标成员,以便在AutoMapper中创建自定义映射? - How to get both mapped source and destination member to create a custom mapping in AutoMapper? 自定义从源转换到目标的 AutoMapper 问题 - AutoMapper problem with custom convert from source to destination Automapper - 目的地的条件映射 - Automapper - conditional mapping on destination 从源映射到现有目标时,AutoMapper 不会忽略 List - AutoMapper does not ignore List when mapping from source to existing destination Automapper - 从源子 object 到目标的映射包括父值 - Automapper - Mapping from source child object to destination is including parent values Automapper 将多个列表从源映射到目标上的单个列表 - Automapper mapping multiple lists from source to single list on destination Automapper 仅在映射对象列表时覆盖不在源中的目标值 - Automapper overrinding destination values that are not in source ONLY when mapping lists of objects AutoMapper:直接映射到子对象目标字段不能按预期工作 - AutoMapper: Direct Mapping to Sub-Object Destination Fields Not Working as Expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM