[英]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.