【发布时间】:2018-05-04 21:37:06
【问题描述】:
我有一个对象:
public class TopLevel{
public string AProperty {get;set;}
public int AnotherProperty {get;set;}
public SecondLevel[] SecondLevels {get;set;}
}
public class SecondLevel{
public string AThing {get;set;}
public int AnotherThing {get;set;}
}
这个我想映射到这个:
public class JoinedClass{
public string AProperty {get;set;}
public int AnotherProperty {get;set;}
public string Athing {get;set;}
public string AnotherThing {get;set;}
}
使用 SecondLevels 数组的 FirstOrDefault 成员。 我认为这是可能的,但似乎无法解决如何做到这一点。
我试过了……
CreateMap<TopLevel, JoinedClass>()
.ForAllMembers(opt=>opt.MapFrom(tl=>tl.SecondLevels.FirstOrDefault())
.ForMemeber(jc=>jc.AProperty, opt=>opt.MapFrom(tl=>tl.AProperty)
.ForMemeber(jc=>jc.AnotherProperty , opt=>opt.MapFrom(tl=>tl.AnotherProperty );
但这似乎根本没有映射任何属性。我还把ForAllMembers() 放在了上面的映射中,也没有运气。
我正在使用 AutoMapper 6.2.0
【问题讨论】:
-
“似乎不起作用”是什么意思,你有什么错误?
标签: mapping asp.net-core-mvc automapper