2015-07-19 57 views
0

以下是我用linq讀取xml數據的方法。現在顯示怎麼做,以獲取分頁數據如何在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

感謝

+1

你已經有使用Skip()然後Take()的分頁邏輯,那有什麼問題? – har07

+0

請更具體。根據Asc使用 - > OrderBy。對於Desc使用OrderByDesc – Kram

+0

當使用Skip()和Take()時,我怎麼能指定爲只有幾個字段獲取數據,稱爲'OrderID,CustomerID,EmployeeID'。請以示例代碼指導我或修改我的代碼。謝謝 – Mou

回答

0

您可以逐步建立您的LINQ到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(); 
+0

非常感謝您的回答。 – Mou

0

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