我在下面的DTO中需要將它映射到一個平面視圖模型,這個想法是通過請求發出的一些屬性是共享的,但可能存在通過名單的名單。AutoMapper從DTO中的列表到單個對象
public class ShinyDTO
{
public List<UserDetails> Users { get; set; }
public string SharedPropertyOne { get; set; }
public string SharedPropertyTwo { get; set; }
}
public class UserDetails
{
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
}
public class MyRealClass
{
public string SharedPropertyOne {get;set;}
public string SharedPropertyTwo {get;set;}
public string Title {get;set;}
public string Forename {get;set;}
public string Surname {get;set;}
}
//This will map all the shared properties
MyRealClass request = Mapper.Map<MyRealClass>(dto);
foreach (var record in dto.Users){
//This bit overwrites the properties set above and then I only have the properties set for Forename, Surname, etc...
request = Mapper.Map<MyRealClass>(record);
}
我需要將其映射到MyRealClass的列表中。我試圖創建單獨的映射,然後在foreach中循環它,但這不斷刪除最初的屬性。
我也嘗試設置第二個映射忽略上面的屬性集,我無法得到這個工作,它仍然覆蓋屬性。
var autoMapperConfiguration = new MapperConfigurationExpression();
autoMapperConfiguration
.CreateMap<MyRealClass, UserDetails>()
.ForMember(c => c.SharedPropertyOne, d => d.Ignore())
.ForMember(c => c.SharedPropertyTwo, d => d.Ignore());
這就是我所需要的 - 你對映射是正確的,這只是我手動鍵入的東西 - 沒有複製和粘貼。 我試圖做mapped = Mapper.Map(record,mapped);重新分配它。 – Kieran