你这个类本身也应该是个泛型类才对哦
public class DataPage<T>
{
public int Total { get; set; }
public List<T> DataList { get; set; }
}
顺便给你个我一直用的分页类
[Serializable]
[DataContract]
public class PagedQuery<TItem> : MultipleQuery<TItem>
{
#region Statics
/// <summary>
/// The default page size.
/// </summary>
public readonly int DefaultPageSize = 10;
#endregion
#region Properties
private long m_PageIndex;
/// <summary>
/// Gets the one-based index of page of current <see cref="PagedQuery<TItem>"/>.
/// </summary>
[DataMember]
public long PageIndex
{
get
{
return m_PageIndex;
}
private set
{
if (value <= 0)
{
m_PageIndex = 1;
}
else
{
m_PageIndex = value;
}
}
}
private int m_PageSize;
/// <summary>
/// Gets the count of items in a page of current <see cref="PagedQuery<TItem>"/>.
/// </summary>
[DataMember]
public int PageSize
{
get
{
return m_PageSize;
}
private set
{
if (value < 0)
{
m_PageSize = DefaultPageSize;
}
else
{
m_PageSize = value;
}
}
}
/// <summary>
/// Gets the total records of current <see cref="PagedQuery<TItem>"/>.
/// </summary>
[DataMember]
public long TotalRecords
{
get;
private set;
}
#endregion
#region Computed Properties
/// <summary>
/// Gets the index of the first item in current page within all objects.
/// </summary>
public long StartIndex
{
get
{
return (checked(((PageIndex - 1) * PageSize) + 1));
}
}
/// <summary>
/// Gets the index of the last item in current page within all objects.
/// </summary>
public long EndIndex
{
get
{
return (checked(StartIndex + PageSize - 1));
}
}
/// <summary>
/// Gets the number of items that should be skipped before reach the first item in current page.
/// </summary>
public long SkipCount
{
get
{
return (checked((PageIndex - 1) * PageSize));
}
}
/// <summary>
/// Gets the number of pages.
/// </summary>
public int PageCount
{
get
{
if (TotalRecords <= 0)
{
return 0;
}
else if (TotalRecords % PageSize > 0)
{
return (checked((int)(TotalRecords / PageSize + 1)));
}
else
{
return (checked((int)(TotalRecords / PageSize)));
}
}
}
/// <summary>
/// Determines whether current page has a previous page.
/// </summary>
public bool HasPreviousPage
{
get
{
return (PageIndex <= 1);
}
}
/// <summary>
/// Determines whether current page has a next page.
/// </summary>
public bool HasNextPage
{
get
{
return (PageIndex >= PageCount);
}
}
#endregion
#region Constructors
/// <summary>
/// Creates an instance of <see cref="PagedQuery<TItem>"/> class.
/// </summary>
/// <param name="pageIndex">The one-based index of page.</param>
/// <param name="pageSize">The count of items in a page.</param>
public PagedQuery(long pageIndex, int pageSize)
{
PageIndex = pageIndex;
PageSize = pageSize;
}
#endregion
#region Methods
/// <summary>
/// Fills current <see cref="PagedQuery<TItem>"/> with result.
/// </summary>
/// <param name="result">The result to fill current <see cref="PagedQuery<TItem>"/></param>
/// <param name="totalRecords">The total records of query.</param>
public void Fill(IEnumerable<TItem> result, long totalRecords)
{
Fill(result);
if (totalRecords < 0)
{
throw new ArgumentOutOfRangeException(
"Never can a query return total records less than zero...", (Exception)null);
}
TotalRecords = totalRecords;
}
/// <summary>
/// Gets the previous page (unfilled).
/// </summary>
/// <returns>The previous page or null if this is the first page.</returns>
public PagedQuery<TItem> GetPreviousPage()
{
return HasPreviousPage ? new PagedQuery<TItem>(PageIndex - 1, PageSize) : (PagedQuery<TItem>)null;
}
/// <summary>
/// Gets the next page (unfilled).
/// </summary>
/// <returns>The next page or null if this is the first page.</returns>
public PagedQuery<TItem> GetNextPage()
{
return HasNextPage ? new PagedQuery<TItem>(PageIndex + 1, PageSize) : (PagedQuery<TItem>)null;
}
/// <summary>
/// Gets the first page (unfilled).
/// </summary>
/// <returns>The first page.</returns>
public PagedQuery<TItem> GetFirstPage()
{
return new PagedQuery<TItem>(1, PageSize);
}
/// <summary>
/// Gets the last page (unfilled).
/// </summary>
/// <returns>The last page.</returns>
public PagedQuery<TItem> GetLastPage()
{
return new PagedQuery<TItem>(PageCount, PageSize);
}
#endregion
}
其类MultipleQuery<T>没什么,就是有一个List<T>的Result属性,这个分页类控制了页数和每页显示的数量肯定不会是负数,同时提供了总页数,是否有前一页,是否有后一页,当前页第一个元素的索引,当前页最后一个元素的索引等等功能,配合写SQL语句方便不少
一般就是用反射了,或者用接口也可以,如果用接口,必须能抽象出统一的接口。
这个类也需要是泛型类,Grace Zhang就是正解!