[英]Custom mapping with Automapper where a field in destination is the concatenation of two fields in source
我認為這個標題已經很好地解釋了這個問題。 我有一個源類型:
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; }
}
和
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; }
}
我想使用automapper從EmployeeViewModel轉換為Employee,Employee的名稱是EmployeeViewModel中名稱和姓氏的串聯。
請您解釋一下如何設置MapperConfiguration? 謝謝!
嘗試這個:
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.