Automapper可以映射分页列表吗? [英] Can Automapper map a paged list?
问题描述
我想使用以下方法将业务对象的分页列表映射到视图模型对象的分页列表:
I'd like to map a paged list of business objects to a paged list of view model objects using something like this:
var listViewModel = _mappingEngine.Map<IPagedList<RequestForQuote>, IPagedList<RequestForQuoteViewModel>>(requestForQuotes);
分页列表的实现与Rob Conery的实现类似: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
The paged list implementation is similar to Rob Conery's implementation here: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
如何设置Automapper来做到这一点?
How can you setup Automapper to do this?
推荐答案
AutoMapper不支持此功能,因为它不知道IPagedList<>
的任何实现.但是,您有两种选择:
AutoMapper does not support this out of the box, as it doesn't know about any implementation of IPagedList<>
. You do however have a couple of options:
-
使用现有的Array/EnumerableMappers作为指南,编写自定义
IObjectMapper
.这就是我个人的做法.
Write a custom
IObjectMapper
, using the existing Array/EnumerableMappers as a guide. This is the way I would go personally.
使用以下命令编写自定义TypeConverter:
Write a custom TypeConverter, using:
Mapper
.CreateMap<IPagedList<Foo>, IPagedList<Bar>>()
.ConvertUsing<MyCustomTypeConverter>();
并在内部使用Mapper.Map
映射列表的每个元素.
and inside use Mapper.Map
to map each element of the list.
这篇关于Automapper可以映射分页列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!