【发布时间】:2021-08-06 17:33:32
【问题描述】:
如何将以下 ItemId(作为源)与 DataId(作为输出)映射:
来源:
public class Source
{
public InputData InputItem { get; set; }
}
public class InputData
{
public int ItemID { get; set; }
}
输出:
public class Output
{
public List<OutputData> OutputItem { get; set; }
}
public class OutputData
{
public string[] DataID { get; set; }
}
我正在尝试通过以下方式对其进行映射:
CreateMap<Source, Output>().ForMember(d => d.OutputItem[0].DataID,
option => option.MapFrom(s => s.InputItem != null ? new string[] { $"item_{s.InputItem.ItemID}" } : null));
获取异常:
Expression 'd => d.OutputItem.get_Item(0).DataID' 必须解析为顶级成员,而不是任何子对象的属性。在子类型或 AfterMap 选项上使用自定义解析器。参数名称:lambdaExpression
谁能帮我映射这些对象。
谢谢
【问题讨论】:
-
可以,但也不能使用 CustomTypeConverter。
标签: c# automapper