1.数据的分布Skip()和take()
Skip(100)代表跳过100行后.从第101行开始分页;
take(10)表示多少行分页;
例:
var result=from pdt in db.Product
select pdt;
this.dataview.DataSource=result.Skip(10).take(10);
this dataview.DataBind();
注:这种方式一次性查询出来所有数据,然后再分页;
2.下面的方式是每个提交时绑定的都是特定的数据,而非所有数据;
public overridevoid DataBind()
{
db.log=this.response.output;
var result=from pdt in db.product
select pdt;
int skipNumber=Int.parse(this.textbox1.text)*10;
this.dataview.DataSource=result.Skip(skipNumber).take(10);
this dataview.DataBind();
}
protected void NextButton_Click(Object sender,EventArgs e)
{
this textbox1.text=(int.parse(this.textbox1.text)+1).ToString);
this.DataBind();
}
protected void form_Load(Object send,EventArgs e)
{
if (!this.IsPostBack)
{
this.DataBind();
}
}
4、
费话不多说,主要是Linq中的Skip及Take这二个扩展方法
int RecordCount = 0;
int PageCount = 1;
DataClassesDataContext data = new DataClassesDataContext();
int SkipIndex = 0;
private string _PageString = "";
public string PageString
{
get {
return _PageString;
}
set {
_PageString = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
RecordCount = data.Orders.Count(); //得到记录数
PageCount = (int)Math.Ceiling((decimal)RecordCount / 10); //以10条记录为一页,计算总页码
int page = 1;
if (!string.IsNullOrEmpty(Request.QueryString["page"]))
page = Convert.ToInt16(Request.QueryString["page"]);
SkipIndex = (page-1) * 10; //计算起始索引,以供后面的Skip扩展方法之用
//拼接分页代码
StringBuilder sb = new StringBuilder();
for (int i = 1; i < PageCount; i++)
{
sb.AppendFormat(@" <a href=""?page={0}"">{0}</a> ",i);
}
_PageString = sb.ToString();
bind();
}
void bind()
{
//奇妙的Linq表达式
var q = (from c in data.Orders select new
{ CustomerID = c.CustomerID, ShipAddress=c.ShipAddress,ID = c.OrderID}
).Skip(SkipIndex).Take(10);
this.GridView1.DataSource = q;
this.GridView1.DataBind();
}
当显示的是第一页时,看linq表达式中为我们动态生成了什么样的语句:
SELECT TOP 10 [t0].[CustomerID], [t0].[ShipAddress], [t0].[OrderID] AS [ID]
FROM [dbo].[Orders] AS [t0]
6、以Northwind为示例数据库,DLINQ(LINQ to SQL)之结合GridView控件和ObjectDataSource控件演示大数据量分页,同时介绍延迟执行和日志记录
示例
PagingAndLogging.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PagingAndLogging.aspx.cs" Inherits="LINQ_DLINQ_PagingAndLogging" Title="大数据量分页、延迟执行和日志记录" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <asp:GridView ID="gvProduct" runat="server" DataSourceID="odsProduct" AllowPaging="True" PageSize="5"> </asp:GridView> <asp:ObjectDataSource ID="odsProduct" runat="server" EnablePaging="True" SelectCountMethod="GetProductCount" SelectMethod="GetProduct" TypeName="PagingAndLogging"> <SelectParameters> <asp:Parameter Name="startRowIndex" Type="Int32" DefaultValue="0" /> <asp:Parameter Name="maximumRows" Type="Int32" DefaultValue="10" /> </SelectParameters> </asp:ObjectDataSource> </asp:Content>
PagingAndLogging.cs
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; using System.ComponentModel; using System.Collections.Generic; using System.IO; using DAL; /// <summary> /// PagingAndLogging 的摘要说明 /// </summary> [DataObject] public class PagingAndLogging { [DataObjectMethod(DataObjectMethodType.Select, true)] public List<Products> GetProduct(int startRowIndex, int maximumRows) { NorthwindDataContext ctx = new NorthwindDataContext(); // System.Data.Linq.DataContext的记录日志的功能 StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "Log.txt", true); ctx.Log = sw; var products = (from p in ctx.Products select p).Skip(startRowIndex).Take(maximumRows); // products实现了IQueryable<T>接口 // 所以可以用如下方法从中获取DbCommand System.Data.Common.DbCommand cmd = ctx.GetCommand(products); string commandText = cmd.CommandText; foreach (System.Data.Common.DbParameter param in cmd.Parameters) { string parameterName = param.ParameterName; object value = param.Value; } // 延迟执行(Deferred Execution) // products实现了IEnumerable<T>接口 // IEnumerable<T>接口的一个特性是,实现它的对象可以把实际的查询运算延迟到第一次对返回值进行迭代(yield)的时候 // ToList()之前,如果是LINQ to SQL的话,那么就可以通过products.ToString()查看LINQ生成的T-SQL // ToList()后则执行运算 var listProducts = products.ToList(); // 执行运算后System.Data.Linq.DataContext会记录日志,所以应该在执行运算后Close掉StreamWriter sw.Flush(); sw.Close(); return listProducts; } public int GetProductCount(int startRowIndex, int maximumRows) { NorthwindDataContext ctx = new NorthwindDataContext(); StreamWriter sw = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath + "Log.txt", true); ctx.Log = sw; // Count查询操作符(不延迟) - 返回集合中的元素个数 int c = (from p in ctx.Products select 0).Count(); sw.Flush(); sw.Close(); return c; } }