缓存使用匿名类型的 LINQ to XML 查询结果的最佳方法是什么?如何转换缓存对象?
我正在使用启用分页的 Gridview。我使用 LINQ to XML 在 Page_Load 事件中绑定 girdview。
代码如下所示。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Cache["grid"] == null)
{
DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("~/CarRentalAddress.xml"));
var grid = (from d in ds.Tables[0].AsEnumerable() orderby d.Field<string>("City") where d.Field<string>("Enabled") == "1" select new { City = d.Field<string>("City"), HotelName = d.Field<string>("HotelName"), Address = d.Field<string>("Address"), EmailID1 = d.Field<string>("EmailID1"), EmailID2 = d.Field<string>("EmailID2") }).ToList();
Cache["grid"] = grid;
totalrows = grid.Count().ToString();
grdAddress.DataSource = grid;
grdAddress.DataBind();
}
else
{
var grid = Cache["grid"];
//Extension method Count() is not working here
totalrows = grid.Count().ToString();
grdAddress.DataSource = grid;
grdAddress.DataBind();
}
}
}
代码的 Else 部分中的扩展方法 Count() 不起作用。我想知道转换缓存对象的最佳方法,以便扩展方法和所有 LINQ 东西都能工作。
I am using a Gridview with paging enabled. I bind the girdview in Page_Load event using LINQ to XML.
The code looks like this.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Cache["grid"] == null)
{
DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("~/CarRentalAddress.xml"));
var grid = (from d in ds.Tables[0].AsEnumerable() orderby d.Field<string>("City") where d.Field<string>("Enabled") == "1" select new { City = d.Field<string>("City"), HotelName = d.Field<string>("HotelName"), Address = d.Field<string>("Address"), EmailID1 = d.Field<string>("EmailID1"), EmailID2 = d.Field<string>("EmailID2") }).ToList();
Cache["grid"] = grid;
totalrows = grid.Count().ToString();
grdAddress.DataSource = grid;
grdAddress.DataBind();
}
else
{
var grid = Cache["grid"];
//Extension method Count() is not working here
totalrows = grid.Count().ToString();
grdAddress.DataSource = grid;
grdAddress.DataBind();
}
}
}
The extension method Count() in Else part of the code is not working. i want to know the best way to cast the cache object so that Extension methods and all LINQ stuffs works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了演示目的,我在网站的根目录中创建了一个 XML 文件,如下所示
CodeBehind
For demo purpose I have created a XML file in the root of website ,something like below
CodeBehind