[C#]WCF 数据服务分页

标签: json WCF Linq C#
发布时间: 2017/2/27 6:48:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

在我的 WCF 服务 tha 分页不乏从数据服务中实现,但不是工作,从激活代码是︰

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

    }

谢谢你的帮助。

解决方法 1:

我发现其他方式,使得分页 WCF 服务...

我曾经发现在其他事儿从 MVC 和改变对我的目的两类......

接口 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);
}
赞助商