Automapper和PagedList

时间:2011-09-03 19:17:46

标签: asp.net-mvc automapper

如何像这样映射列表?我的CreateMap看起来如何? PagedList的类如下所示:

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;
        }
    }
}

我的地图代码:

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

当我执行上述操作时,仅映射列表,其他属性(如TotalCount,PageSize)未映射。

1 个答案:

答案 0 :(得分:0)

无论您提出什么解决方案,都需要枚举您的原始列表。你能做什么,是这样的:

var modelList = new PagedList<UserModel>(
    userList.Select(u => Mapper.Map<User, UserModel>(u)).AsQueryable(), 
    index, pageSize);