簡體   English   中英

使用Automapper進行自定義映射,其中目標中的字段是源中兩個字段的串聯

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM