如何在LINQ读取xml数据时进行分页

本文关键字:分页 数据 xml LINQ 读取 | 更新日期: 2023-09-27 18:03:21

下面我用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指定订单

谢谢

如何在LINQ读取xml数据时进行分页

您可以增量地构建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();

所以我更新的代码如下,我从@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();