最后使用LINQ和retun数据表读取XML文件

时间:2015-07-19 11:26:17

标签: c# xml linq

请参阅下面的内联SQL代码:

public DataTable SupplyPageOfData(int lowerPageBoundary, int rowsPerPage)
{
    // Store the name of the ID column. This column must contain unique  
    // values so the SQL below will work properly. 
    if (columnToSortBy == null)
    {
        columnToSortBy = this.Columns[0].ColumnName;
    }

    if (!this.Columns[columnToSortBy].Unique)
    {
        throw new InvalidOperationException(String.Format(
            "Column {0} must contain unique values.", columnToSortBy));
    }

    // Retrieve the specified number of rows from the database, starting 
    // with the row specified by the lowerPageBoundary parameter.
    command.CommandText = "Select Top " + rowsPerPage + " " +
        CommaSeparatedListOfColumnNames + " From " + tableName +
        " WHERE " + columnToSortBy + " NOT IN (SELECT TOP " +
        lowerPageBoundary + " " + columnToSortBy + " From " +
        tableName + " Order By " + sortColumn + 
        ") Order By " + sortColumn ;
    adapter.SelectCommand = command;

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);
    return table;
}

此例程在订单表上进行分页,并在最后返回数据表。

1)当我将通过linq从xml文件查询相同的数据时,我怎样才能通过linq编写相同的查询。那里有order by子句,那里将有分页。

因此,请告诉我如何为订单和分页自定义以下linq查询

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

2)我有另一个例程,它将读取xml文件并使用linq从xml文件返回总记录数

请用代码示例指导我。感谢

修改

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();

而不是数据表我看到我们可以将列表绑定到网格。感谢

0 个答案:

没有答案