缓存使用匿名类型的 LINQ to XML 查询结果的最佳方法是什么?如何转换缓存对象?

发布于 2024-10-20 15:57:45 字数 1312 浏览 2 评论 0原文

我正在使用启用分页的 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 技术交流群。

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

发布评论

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

评论(1

就此别过 2024-10-27 15:57:45

为了演示目的,我在网站的根目录中创建了一个 XML 文件,如下所示

<?xml version="1.0" encoding="utf-8" ?>
<Persons>
  <Person>
    <FirstName>F1</FirstName>
    <LastName>L1</LastName>
    <Age>31</Age>
  </Person>
  <Person>
    <FirstName>F2</FirstName>
    <LastName>L2</LastName>
    <Age>22</Age>
  </Person>
  <Person>
    <FirstName>F3</FirstName>
    <LastName>L3</LastName>
    <Age>25</Age>
  </Person>
</Persons>

CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Cache["grid"]== null)
            {

                var grid = (from d in GetData().Tables[0].AsEnumerable()
                            orderby d.Field<string>("FirstName")
                            where d.Field<string>("Age") == "31"
                            select new Person()
                            {
                                FirstName = d.Field<string>("FirstName"),
                                LastName = d.Field<string>("LastName"),
                                Age = d.Field<string>("Age")

                            }).ToList();

                Cache["grid"] = grid;
                int totalrows = grid.Count();

                GridView1.DataSource = grid;
                GridView1.DataBind();
            }
            else
            {
                var grid = (List<Person>)Cache["grid"];
                //Extension method Count() is not working here
                int totalrows = grid.Count();
                GridView1.DataSource = grid;
                GridView1.DataBind();
            }
        }

    }

    public static DataSet GetData()
    {

        DataSet ds = new DataSet();
        ds.ReadXml(HttpContext.Current.Server.MapPath("Person.xml"));
        return ds;

    }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }

}

For demo purpose I have created a XML file in the root of website ,something like below

<?xml version="1.0" encoding="utf-8" ?>
<Persons>
  <Person>
    <FirstName>F1</FirstName>
    <LastName>L1</LastName>
    <Age>31</Age>
  </Person>
  <Person>
    <FirstName>F2</FirstName>
    <LastName>L2</LastName>
    <Age>22</Age>
  </Person>
  <Person>
    <FirstName>F3</FirstName>
    <LastName>L3</LastName>
    <Age>25</Age>
  </Person>
</Persons>

CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Cache["grid"]== null)
            {

                var grid = (from d in GetData().Tables[0].AsEnumerable()
                            orderby d.Field<string>("FirstName")
                            where d.Field<string>("Age") == "31"
                            select new Person()
                            {
                                FirstName = d.Field<string>("FirstName"),
                                LastName = d.Field<string>("LastName"),
                                Age = d.Field<string>("Age")

                            }).ToList();

                Cache["grid"] = grid;
                int totalrows = grid.Count();

                GridView1.DataSource = grid;
                GridView1.DataBind();
            }
            else
            {
                var grid = (List<Person>)Cache["grid"];
                //Extension method Count() is not working here
                int totalrows = grid.Count();
                GridView1.DataSource = grid;
                GridView1.DataBind();
            }
        }

    }

    public static DataSet GetData()
    {

        DataSet ds = new DataSet();
        ds.ReadXml(HttpContext.Current.Server.MapPath("Person.xml"));
        return ds;

    }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }

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