【发布时间】:2012-03-15 21:08:10
【问题描述】:
我有链表的情况。我的 DTO 看起来像这样 -
public class DTOItem
{
public string ID
{
get;
set;
}
public int? UniqueId
{
get;
set;
}
public string Payload
{
get;
set;
}
//How do I map this guy? It is list of same type.
public List<DTOItem> RelatedItems
{
get;
set;
}
}
如何使用 AutoMapper 映射这个人?我能够映射班级的其他成员。数据是从另一个类的集合对象映射的,该集合对象具有与该类不同的一组不同的成员。
public List<DTOItem> RelatedItems
{
get;
set;
}
提前致谢。
更新:这是代码 - 拉斐尔,这是代码:
源对象:
public class ResultsSet
{
public int? ResultId
{
get;
set;
}
public int UID
{
get;
set;
}
//Returns large XML string
public string ResultBlob
{
get;
set;
}
public RelatedItems[] RelatedSet
{
get;
set;
}
}
public class RelatedItems
{
public int Item_ID
{
get;
set;
}
public int Relationship_ID
{
get;
set;
}
public string Description
{
get;
set;
}
}
这里的映射是代码:
Mapper.CreateMap<ResultSet, DTOItem>()
.ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.ResultID.GetValueOrDefault(0)))
.ForMember(dest => dest.UniqueId, opt => opt.MapFrom(src => src.UID))
.ForMember(dest => dest.Payload, opt => opt.MapFrom(src => src.ResultBlob));
/*
How do I map RelatedSet to RelatedItems here?
*/
Mapper.Map(result, returnResult);
再次感谢。
【问题讨论】:
-
你能给出“源”类和你尝试了什么(映射)吗?
-
嗨 Raphael,我已经添加了源类。
-
这不是链表……这个数据里有循环引用吗?
-
是的,你说得对,这不是一个链表。它更像是一棵树。不,不会有任何循环引用。 RelatedSet 在集合中可以有零个或多个。而且,RelatedSet 集合中的项目将不再有子 DTOItem。
标签: c# automapper