Search code examples
c#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);

The paged list implementation is similar to Rob Conery's implementation here: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

How can you setup Automapper to do this?


Solution

  • 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:

    1. Write a custom IObjectMapper, using the existing Array/EnumerableMappers as a guide. This is the way I would go personally.

    2. Write a custom TypeConverter, using:

      Mapper
          .CreateMap<IPagedList<Foo>, IPagedList<Bar>>()
          .ConvertUsing<MyCustomTypeConverter>();
      

      and inside use Mapper.Map to map each element of the list.