【发布时间】: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