asp.net mvc中基于两个参数使用Linq-To-Sql进行分页

发布于 2024-08-31 05:05:15 字数 1881 浏览 11 评论 0原文

作为两个参数,我说 currentPagepagesize .....到目前为止,我使用了 sql server 存储过程并实现了这样的分页,

GO
ALTER PROCEDURE [dbo].[GetMaterialsInView]
    -- Add the parameters for the stored procedure here
    @CurrentPage INT,
    @PageSize INT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

     SELECT *,ROW_NUMBER() OVER (ORDER BY Id) AS Row FROM    
     (
     SELECT
      *,ROW_NUMBER() OVER (ORDER BY Id) AS Row
      FROM InTimePagingView
     ) AS InTimePages
    WHERE  Row >= (@CurrentPage - 1) * @PageSize + 1 AND Row <= @CurrentPage*@PageSize

    SELECT COUNT(*) as TotalCount FROM InTimePagingView

    SELECT  CEILING(COUNT(*) / CAST(@PageSize AS FLOAT)) NumberOfPages
     FROM  InTimePagingView

END

现在我正在使用 Linq-to-sql我使用这个,

public IQueryable<MaterialsObj> FindAllMaterials()
        {
           var materials =  from m in db.Materials
                   join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
                   where m.Is_Deleted == 0
                   select new MaterialsObj()
                   {
                       Id = Convert.ToInt64(m.Mat_id),
                       Mat_Name = m.Mat_Name,
                       Mes_Name = Mt.Name,
                   };
            return materials;

        }

现在我想返回记录,TotalCount,其中我使用总计数生成页码.....这可能吗...任何建议...

编辑:刚刚

发现这个...

NorthWindDataContext db = new NorthWindDataContext();

var query = from c in db.Customers
            select c.CompanyName;

//Assuming Page Number = 2, Page Size = 10
int iPageNum = 2;
int iPageSize = 10;

var PagedData = query.Skip((iPageNum - 1) * iPageSize).Take(iPageSize);

ObjectDumper.Write(PagedData);

As two parameters i say currentPage and pagesize .....I thus far used sql server stored procedures and implemented paging like this,

GO
ALTER PROCEDURE [dbo].[GetMaterialsInView]
    -- Add the parameters for the stored procedure here
    @CurrentPage INT,
    @PageSize INT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

     SELECT *,ROW_NUMBER() OVER (ORDER BY Id) AS Row FROM    
     (
     SELECT
      *,ROW_NUMBER() OVER (ORDER BY Id) AS Row
      FROM InTimePagingView
     ) AS InTimePages
    WHERE  Row >= (@CurrentPage - 1) * @PageSize + 1 AND Row <= @CurrentPage*@PageSize

    SELECT COUNT(*) as TotalCount FROM InTimePagingView

    SELECT  CEILING(COUNT(*) / CAST(@PageSize AS FLOAT)) NumberOfPages
     FROM  InTimePagingView

END

Now i am using Linq-to-sql and i use this,

public IQueryable<MaterialsObj> FindAllMaterials()
        {
           var materials =  from m in db.Materials
                   join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
                   where m.Is_Deleted == 0
                   select new MaterialsObj()
                   {
                       Id = Convert.ToInt64(m.Mat_id),
                       Mat_Name = m.Mat_Name,
                       Mes_Name = Mt.Name,
                   };
            return materials;

        }

Now i want to return the records,TotalCount where i use Total count to generate pagenumbers..... Is this possible... Any suggestion...

EDIT:

Just found this...

NorthWindDataContext db = new NorthWindDataContext();

var query = from c in db.Customers
            select c.CompanyName;

//Assuming Page Number = 2, Page Size = 10
int iPageNum = 2;
int iPageSize = 10;

var PagedData = query.Skip((iPageNum - 1) * iPageSize).Take(iPageSize);

ObjectDumper.Write(PagedData);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-09-07 05:05:15

您可以使用 IQueryable.Skip()、.Take() 和 .Count() “手动”完成此操作,或者您可以使用这个方便的模式:-

public class PagedList<T> : List<T>
{
    public PagedList(IEnumerable<T> source, int index, int pageSize)
    {
        this.TotalCount = source.Count();
        this.PageSize = pageSize;
        this.PageIndex = index;
        this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
    }    

    public int TotalCount { get; set; }
    public int PageIndex { get; set; }
    public int PageSize { get; set; }

    public bool HasPreviousPage 
    { 
        get 
        {
            return (PageIndex > 0);
        }
    }

    public bool HasNextPage 
    { 
        get
        {
            return ((PageIndex + 1) * PageSize) < TotalCount;
        } 
    }     
 }

像这样使用它:-

public ActionResult Materials(int page)
{
  var materials = MaterialsRepository.FindAllMaterials();
  var results = new PagedList<MaterialsObj>(materials, page - 1, 10);
  return new View(results)
}

互联网上有很多实现有帮助者、测试和所有这些爵士乐。我认为原始代码是 ScottGu 提出的,但不要引用我的话。

You can do it "by hand" with IQueryable.Skip(), .Take(), and .Count(), or you can use this handy pattern:-

public class PagedList<T> : List<T>
{
    public PagedList(IEnumerable<T> source, int index, int pageSize)
    {
        this.TotalCount = source.Count();
        this.PageSize = pageSize;
        this.PageIndex = index;
        this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
    }    

    public int TotalCount { get; set; }
    public int PageIndex { get; set; }
    public int PageSize { get; set; }

    public bool HasPreviousPage 
    { 
        get 
        {
            return (PageIndex > 0);
        }
    }

    public bool HasNextPage 
    { 
        get
        {
            return ((PageIndex + 1) * PageSize) < TotalCount;
        } 
    }     
 }

Use it like this:-

public ActionResult Materials(int page)
{
  var materials = MaterialsRepository.FindAllMaterials();
  var results = new PagedList<MaterialsObj>(materials, page - 1, 10);
  return new View(results)
}

There are a bunch of implementations around the internet with helpers and tests and all that jazz. I think it was ScottGu that came up with the original code, but don't quote me on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文