簡體   English   中英

WCF數據服務分頁

[英]WCF Data Services Pagination

我在WCF服務中通過數據服務實現分頁,但無法正常工作,激活代碼如下:

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

    }
}

我使用EF 6 + Linq

我確實可以毫無問題地檢索所有產品,但是我需要分頁,因為它是一個大數據。

謝謝

我的查詢代碼:

    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");

    }

感謝您的幫助。

我找到了分頁使用WCF服務的其他方法...

我使用了MVC在其他proyect中找到的兩個類,並改變了目的。

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

分頁類

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
}

我的運營合同(類)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM