我想使用如下内容将业务对象的分页列表映射到视图模型对象的分页列表:
var listViewModel = _mappingEngine.Map<IPagedList<RequestForQuote>, IPagedList<RequestForQuoteViewModel>>(requestForQuotes);
分页列表的实现类似于Rob Conery的实现:http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
您如何设置Automapper来执行此操作?
发布于 2012-09-18 00:18:54
使用jrummell的答案,我创建了一个适用于Troy Goode's PagedList的扩展方法。它让你不必把这么多代码放在任何地方……
public static IPagedList<TDestination> ToMappedPagedList<TSource, TDestination>(this IPagedList<TSource> list)
{
IEnumerable<TDestination> sourceList = Mapper.Map<IEnumerable<TSource>, IEnumerable<TDestination>>(list);
IPagedList<TDestination> pagedResult = new StaticPagedList<TDestination>(sourceList, list.GetMetaData());
return pagedResult;
}
用法为:
var pagedDepartments = database.Departments.OrderBy(orderBy).ToPagedList(pageNumber, pageSize).ToMappedPagedList<Department, DepartmentViewModel>();
发布于 2012-03-09 23:03:32
如果您使用的是Troy Goode's PageList,有一个StaticPagedList
类可以帮助您进行映射。
// get your original paged list
IPagedList<Foo> pagedFoos = _repository.GetFoos(pageNumber, pageSize);
// map to IEnumerable
IEnumerable<Bar> bars = Mapper.Map<IEnumerable<Bar>>(pagedFoos);
// create an instance of StaticPagedList with the mapped IEnumerable and original IPagedList metadata
IPagedList<Bar> pagedBars = new StaticPagedList<Bar>(bars, pagedFoos.GetMetaData());
发布于 2021-12-14 22:31:40
使用Automapper .net核心8.1.1很容易实现
您只需将类型映射添加到mapperProfile并在pagedList中映射对象
CreateMap(typeof(IPagedList<>), typeof(IPagedList<>));
CreateMap<RequestForQuote, RequestForQuoteViewModel>().ReverseMap();
它也可以是像PagedList这样的抽象类。与类/ı接口类型无关
您可以直接在类构造函数的IMapper中的mapper.Map - Initialize mapper中使用它
RequestForQuote result
_mapper.Map<IPagedList<RequestForQuoteViewModel>>(result);
https://stackoverflow.com/questions/2070850
复制相似问题