作为两个参数我说currentPage和pagesize …..我到目前为止使用sql server存储过程并实现这样的分页,GOALTER PROCEDURE [dbo].[GetMaterialsInView]-- Add the parameters for the stored procedure here@CurrentPag...
作为两个参数我说currentPage和pagesize …..我到目前为止使用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);
解决方法:
你可以使用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提出了原始代码,但不要引用我的话.
> http://pagedlist.codeplex.com/
> http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/
> http://blog.wekeroad.com/blog/aspnet-mvc-pagedlistt/
> http://mha.dk/post/LINQ-and-PagedList.aspx
本文标题为:c# – 基于asp.net mvc中的两个参数使用Linq-To-Sql进行分页
基础教程推荐
- C#实现DataTable数据行列转换 2023-06-04
- C#流程控制详解 2023-06-21
- C#关键字async/await用法 2023-02-03
- C#并行编程之Task任务 2023-06-05
- C#用记事本编写简单WinForm窗体程序 2023-01-22
- 详解C# 利用反射根据类名创建类的实例对象 2022-11-05
- C#中多线程ManualResetEvent 与 AutoResetEvent 区别 2023-03-04
- Unity游戏开发中的中介者模式的应用与实现 2023-07-19
- c# 使用计时器和观察者模式实现报警推送需求 2023-03-04
- C# RabbitMQ的使用详解 2023-05-12