【问题标题】:WCF Data Services PaginationWCF 数据服务分页
【发布时间】:2015-03-19 03:16:49
【问题描述】:

我在我的 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");

    }

感谢您的帮助。

【问题讨论】:

    标签: c# json linq wcf pagination


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2011-04-22
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多