下面的代码演示了如何利用Linq结合AspNetPager控件实现分页功能,以及如果利用Linq to Xml将当前页数据保存为Xml
using (NorthWindDataContext db = new NorthWindDataContext())
{
int Page = Utils.GetPageIndex();//取得当前页码
var s = from c in db.T_Tests orderby c.F_AutoId select new { c.F_ID,F_AutoID = c.F_AutoId.ToString().PadLeft(8,'0')};
var p = s.Skip((Page - 1) * this.AspNetPager1.PageSize).Take(this.AspNetPager1.PageSize);//取得当前页数据(注:先跳过(Page-1)*PageSize条记录后,再取PageSize条记录)
this.GridView3.DataSource = p;
this.GridView3.DataBind();
this.AspNetPager1.RecordCount = s.Count();//设置分页控件的总记录数
this.AspNetPager1.CurrentPageIndex = Page;//设置分页控件的当前页
//将当前数据保存为xml
Xdocument doc = new Xdocument(new XElement(
"T_Test", from d in p
select
(
new XElement
(
"data",
new XAttribute("F_ID", d.F_ID),
new XAttribute("F_AutoID", d.F_AutoID)
)
)
)
);
doc.Save("c:\demo.xml");
db.Dispose();//及时释放资源
}
保存的Demo.Xml内容如下:



