Can Automapper map a paged list?

前端 未结 6 1898
我寻月下人不归
我寻月下人不归 2021-02-01 19:38

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

        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 20:14

    Using jrummell's answer, I created an extension method that works with Troy Goode's PagedList. It keeps you from having to put so much code everywhere...

        public static IPagedList ToMappedPagedList(this IPagedList list)
        {
            IEnumerable sourceList = Mapper.Map, IEnumerable>(list);
            IPagedList pagedResult = new StaticPagedList(sourceList, list.GetMetaData());
            return pagedResult;
    
        }
    

    Usage is:

    var pagedDepartments = database.Departments.OrderBy(orderBy).ToPagedList(pageNumber, pageSize).ToMappedPagedList();
    

提交回复
热议问题