简体   繁体   中英

WCF Data Services Pagination

I implement in my WCF Services tha pagination throught Data Service but not work, the code from the activation is:

public class Profit : DataService<ProfitEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // Set page size defaults for the data service.
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;            
        config.SetEntitySetPageSize("artEntities", 1);

    }
}

I use EF 6 + Linq

I retrieve all my Product indeed without a problem but I need the pagination beacuse is a large data.

Thanks you

Code of my Query:

    public List<Product> GetAllProduct()
    {
        ProfitEntities context = new ProfitEntities();

        List<Product> product = new List<Product>();
        foreach (artEntities art in context.art)
        {
            product.Add(new Product
            {
                coArt = art.co_art.Trim(),
                desArt = art.art_des.Trim(),
                stockAct = Convert.ToDecimal(art.stock_act),
                precVta1 = Convert.ToDecimal(art.prec_vta1),
                anulado = art.anulado
            });

        }


        if (product.Count > 0)
        {
            return product;
        }
        else
            throw new Exception("Imposible conseguir lista de Articulos");

    }

Thanks you for your help.

I found other way to make the pagination to WCF Services...

I used a two class found in other proyect from MVC and change to my purpose...

Interface IPagedList

public interface IPagedList<T> : IList<T>
{
    int PageCount { get; }
    int TotalItemCount { get; }
    int PageIndex { get; }
    int PageNumber { get; }
    int PageSize { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
    bool IsFirstPage { get; }
    bool IsLastPage { get; }
}

Paged Class

public class PagedList<T> : List<T>, IPagedList<T>
{
    public PagedList(IEnumerable<T> source, int index, int pageSize, int? totalCount = null)
        : this(source.AsQueryable(), index, pageSize, totalCount)
    {
    }

    public PagedList(IQueryable<T> source, int index, int pageSize, int? totalCount = null)
    {
        if (index < 0)
            throw new ArgumentOutOfRangeException("index", "Value can not be below 0.");
        if (pageSize < 1)
            throw new ArgumentOutOfRangeException("pageSize", "Value can not be less than 1.");

        if (source == null)
            source = new List<T>().AsQueryable();

        var realTotalCount = source.Count();

        PageSize = pageSize;
        PageIndex = index;
        TotalItemCount = totalCount.HasValue ? totalCount.Value : realTotalCount;
        PageCount = TotalItemCount > 0 ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) : 0;

        HasPreviousPage = (PageIndex > 0);
        HasNextPage = (PageIndex < (PageCount - 1));
        IsFirstPage = (PageIndex <= 0);
        IsLastPage = (PageIndex >= (PageCount - 1));

        if (TotalItemCount <= 0)
            return;

        var realTotalPages = (int)Math.Ceiling(realTotalCount / (double)PageSize);

        if (realTotalCount < TotalItemCount && realTotalPages <= PageIndex)
            AddRange(source.Skip((realTotalPages - 1) * PageSize).Take(PageSize));
        else
            AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
    }

    #region IPagedList Members

    public int PageCount { get; private set; }
    public int TotalItemCount { get; private set; }
    public int PageIndex { get; private set; }
    public int PageNumber { get { return PageIndex + 1; } }
    public int PageSize { get; private set; }
    public bool HasPreviousPage { get; private set; }
    public bool HasNextPage { get; private set; }
    public bool IsFirstPage { get; private set; }
    public bool IsLastPage { get; private set; }

    #endregion
}

My Operation Contract (Class)

private int defaultPageSize = 10;
ProfitEntities context = new ProfitEntities();
public List<Product> GetAllProduct(string value)
{

   var artprofit = context.art.Include("colores").Include("lin_art").Include("sub_lin").Include("cat_art").ToList();

   int? page = Convert.ToInt32(value);
   int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
   var productListPaged = artprofit.ToPagedList(currentPageIndex, defaultPageSize);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM