0
試圖映射以下拼合嵌套列表Automapper
public class WorkPreferance
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<Location> PreferedLocations { get; set; }
}
public class Location
{
public int LocationID { get; set; }
public string LocationName { get; set; }
}
到以下目標...
public class WorkingPreferenceViewModel
{
public int ID { get; set; }
public string Name { get; set; }
public int LocationID { get; set; }
public string LocationName { get; set; }
}
,但我不能換我的頭周圍應該是什麼映射減速.. 所以遠我有這個:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Location, WorkingPreferenceViewModel>()
.ForMember(d => d.LocationID, o => o.MapFrom(s => s.LocationID))
.ForMember(d => d.LocationName, o => o.MapFrom(s => s.Description))
.ForAllOtherMembers(o => o.Ignore());
cfg.CreateMap<WorkPreferance, WorkingPreferenceViewModel>()
.ForMember(d => d.LocationID, o => o.MapFrom(s => s.PreferedLocations))
.ForMember(d => d.LocationName, o => o.MapFrom(s => s.PreferedLocations))
}
我很困惑。你不能將任何東西的集合映射到該事物的一個實例。除非你做某種聚合或從集合中獲取單個實例。當你映射'WorkPreference'時,'LocationID'和'LocationName'應該是什麼? – MarioDS
你可以當你想扁平化..就像你在LINQ中使用SelectMany .. – Mortalus
對每個位置條目的意義我想創建一個新的對象WorkingPreferanceViewModel ... – Mortalus