【问题标题】:How to do the pagination when read xml data by LINQ通过LINQ读取xml数据时如何进行分页
【发布时间】:2015-10-08 15:55:07
【问题描述】:

以下方式我正在使用 linq 读取 xml 数据。现在展示如何使用分页获取数据

XDocument document = XDocument.Load(@"c:\users\tridip\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
            var books = from r in document.Descendants("Orders")
            select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

我得到了一个示例脚本,但它看起来有点不同。这是代码

var limit=100;

var items = xmldoc.Descendants("whatevernodename")
           .Select(node => node.Value.ToString())
           .Skip(limit)
           .Take(100)
           .ToList();

如果看到上面的代码,你可以看到所有字段都会返回,但是下面这种方式我需要指定字段

select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

还告诉我如何通过订单 ID ASC 或 DESC 指定订单

谢谢

【问题讨论】:

  • 你已经有了分页逻辑,使用Skip()然后Take(),有什么问题?
  • 请更具体。根据 Asc 使用 -> OrderBy。对于 Desc 使用 OrderByDesc
  • 当使用Skip() and Take() 时,我如何指定仅获取名为OrderID, CustomerID, EmployeeID 的少数字段的数据。请用示例代码指导我或修改我的代码。谢谢

标签: c# xml linq pagination


【解决方案1】:

您可以增量构建 LINQ-to-XML 查询,例如:

//setup basic query
var query = from r in document.Descendants("Orders")
            select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

//setup query result ordering,
//assume we have variable to determine ordering mode : bool isDesc = true/false
if (isDesc) query = query.OrderByDescending(o => o.OrderID);
else query = query.OrderBy(o => o.OrderID);

//setup pagination, 
//f.e displaying result for page 2 where each page displays 100 data
var page = 2;
var pageSize = 100;
query = query.Skip(page - 1*pageSize).Take(pageSize);

//execute the query to get the actual result
var items = query.ToList();

【讨论】:

    【解决方案2】:

    所以我的更新代码如下,我在得到@Har07 的回答后修复了。

    XDocument document = XDocument.Load(@"c:\users\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
                bool isDesc = true;
                //setup basic query
                var query = from r in document.Descendants("Orders")
                select new
                {
                    OrderID = r.Element("OrderID").Value,
                    CustomerID = r.Element("CustomerID").Value,
                    EmployeeID = r.Element("EmployeeID").Value,
                };
    
                //setup query result ordering,
                //assume we have variable to determine ordering mode : bool isDesc = true/false
                if (isDesc) query = query.OrderByDescending(o => o.OrderID);
                else query = query.OrderBy(o => o.OrderID);
    
                //setup pagination, 
                //f.e displaying result for page 2 where each page displays 100 data
                var page = 1;
                var pageSize = 5;
                query = query.Skip(page - 1 * pageSize).Take(pageSize);
    
                //execute the query to get the actual result
                //var items = query.ToList();
                dataGridView1.DataSource = query.ToList();
    

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2011-03-22
      • 1970-01-01
      • 2015-03-10
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多