C# Automapper 6客户列表<;t来源>;自定义列表2<;TDest>;不映射项目
我有两种C# Automapper 6客户列表<;t来源>;自定义列表2<;TDest>;不映射项目,c#,automapper-6,C#,Automapper 6,我有两种PagedList类型数据。页面列表实现IList,视图模型。页面列表继承列表。我正在尝试从Data.PagedList映射到ViewModels.PagedList。我有两个PagedList的开放通用映射和Comments->CommentsVM的映射,但映射后,ViewModels.PagedList设置了所有属性,但它包含0项 数据。页面列表: 我的期望是,因为automapper可以将列表映射到列表,所以一旦我添加了页面列表之间的开放泛型映射,它就可以在这里实现同样的功能。为什
PagedList
类型<代码>数据。页面列表实现IList
,视图模型。页面列表
继承列表
。我正在尝试从Data.PagedList
映射到ViewModels.PagedList
。我有两个PagedList的开放通用映射和Comments->CommentsVM的映射,但映射后,ViewModels.PagedList
设置了所有属性,但它包含0项
数据。页面列表
:
我的期望是,因为automapper可以将列表映射到列表,所以一旦我添加了页面列表之间的开放泛型映射,它就可以在这里实现同样的功能。为什么它不自动映射列表项?可以吗?我想默认情况下automapper会将列表映射到另一个列表。如果删除
CreateMap(typeof(Data.PagedList)、typeof(ViewModels.PagedList))从映射配置中选择code>,然后映射对象,您将看到注释列表将正确映射,但是您定义的属性显然不会正确映射。因此,我们可以假设,当定义映射时,列表到列表的默认行为将不会生效,需要提供完整的映射定义。
public class PagedList<T> : IList<T>
{
private IList<T> _innerList;
private int _totalCount;
private int _pageSize;
private int _pageNumber;
public PagedList()
{
_innerList = new List<T>();
}
public PagedList(IList<T> existingList)
{
_innerList = existingList;
}
public int TotalCount
{
get { return _totalCount; }
set { _totalCount = value; }
}
public int PageSize
{
get { return _pageSize; }
set { _pageSize = value; }
}
public int PageNumber
{
get { return _pageNumber; }
set { _pageNumber = value; }
}
//IList implementation...
}
public class PagedList<T> : List<T>
{
public PagedList()
{
}
public PagedList(IEnumerable<T> collection) : base(collection) { }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
}
CreateMap(typeof(Data.PagedList<>), typeof(ViewModels.PagedList<>));
CreateMap<Comments, CommentsVM>();
{
IMapper mapper = MapperConfig.EntityWebMapper;
Data.PagedList<Comments> comments = Repository.GetAccountComments(accountID, pageNum, pageSize);
var result = mapper.Map<ViewModels.PagedList<CommentsVM>>(comments);
foreach (var c in comments)
result.Add(mapper.Map<CommentsVM>(c));
return result;
}