1

How can I map a List like this? How would my CreateMap look? The class for PagedList looks like this:

public interface IPagedList
{
    int TotalCount
    {
        get;
        set;
    }

    int PageIndex
    {
        get;
        set;
    }

    int PageSize
    {
        get;
        set;
    }

    bool IsPreviousPage
    {
        get;
    }

    bool IsNextPage
    {
        get;
    }
}

public class PagedList<T> : List<T>, IPagedList
{

    public PagedList(IQueryable<T> source, int index, int pageSize)
    {
        this.TotalCount = source.Count();
        this.PageSize = pageSize;
        this.PageIndex = index;
        this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
    }

    public PagedList(List<T> source, int index, int pageSize)
    {
        this.TotalCount = source.Count();
        this.PageSize = pageSize;
        this.PageIndex = index;
        this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
    }

    public PagedList()
    {
    }

    public int TotalCount
    {
        get;
        set;
    }

    public int PageIndex
    {
        get;
        set;
    }

    public int PageSize
    {
        get;
        set;
    }

    public bool IsPreviousPage
    {
        get
        {
            return (PageIndex > 0);
        }
    }

    public bool IsNextPage
    {
        get
        {
            return (PageIndex * PageSize) <= TotalCount;
        }
    }
}

My mapping code:

Mapper.CreateMap<User, UserModel>();
var model = Mapper.Map<PagedList<User>, PagedList<UserModel>>(users); // Not quite sure about this.

When I do the above, Only the list gets mapped, the other properties such as TotalCount, PageSize, are not mapped.

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • How does the `User` and `UserModel` class look like? That's what you are trying to map at the end and you haven't shown any of them. – Darin Dimitrov Sep 03 '11 at 22:12
  • @Darin Dimitrov They can be anything, they map normally, just problem when I put them in this PagedList. – Shawn Mclean Sep 03 '11 at 22:49
  • This topic discusses exactly this issue: http://stackoverflow.com/questions/2070850/can-automapper-map-a-paged-list – ekenman Mar 19 '12 at 18:23

1 Answers1

0

Whatever solution you come up with, there will be a need to enumerate your original list. What you could do, is something like this:

var modelList = new PagedList<UserModel>(
    userList.Select(u => Mapper.Map<User, UserModel>(u)).AsQueryable(), 
    index, pageSize);
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129