基于方法的查询语法示例:排序 (LINQ to DataSet)
更新:November 2007
本主题中的示例演示如何使用 OrderBy、Reverse<TSource> 和 ThenBy 方法来查询 DataSet 并使用方法查询语法对结果排序。
这些示例中所使用的 FillDataSet 方法已在 向数据集中加载数据 中指定。
本主题中的示例使用 AdventureWorks 示例数据库中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 表。
本主题中的示例使用下面的 using/Imports 语句:
Option Explicit On
Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Globalization
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Globalization;
有关更多信息,请参见如何:在 Visual Studio 中创建 LINQ to DataSet 项目。
OrderBy
示例
此示例使用具有自定义比较器的 OrderBy 方法对姓氏执行不区分大小写的排序。
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim contacts As DataTable = ds.Tables("Contact")
Dim query As IEnumerable(Of DataRow) = _
contacts.AsEnumerable().OrderBy(Function(contact) _
contact.Field(Of String)("LastName"), _
New CaseInsensitiveComparer())
For Each contact As DataRow In query
Console.WriteLine(contact.Field(Of String)("LastName"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable contacts = ds.Tables["Contact"];
IEnumerable<DataRow> query =
contacts.AsEnumerable().OrderBy(contact => contact.Field<string>("LastName"),
new CaseInsensitiveComparer());
foreach (DataRow contact in query)
{
Console.WriteLine(contact.Field<string>("LastName"));
}
Reverse
示例
此示例使用 Reverse<TSource> 方法创建 OrderDate 早于 2002 年 2 月 20 日的订单的列表。
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim orders As DataTable = ds.Tables("SalesOrderHeader")
Dim query = ( _
From order In orders.AsEnumerable() _
Where order.Field(Of DateTime)("OrderDate") < New DateTime(2002, 2, 20) _
Select order).Reverse()
Console.WriteLine("A backwards list of orders where OrderDate < Feb 20, 2002")
For Each order In query
Console.WriteLine(order.Field(Of DateTime)("OrderDate"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable orders = ds.Tables["SalesOrderHeader"];
IEnumerable<DataRow> query = (
from order in orders.AsEnumerable()
where order.Field<DateTime>("OrderDate") < new DateTime(2002, 02, 20)
select order).Reverse();
Console.WriteLine("A backwards list of orders where OrderDate < Feb 20, 2002");
foreach (DataRow order in query)
{
Console.WriteLine(order.Field<DateTime>("OrderDate"));
}
ThenBy
示例
此示例使用具有自定义比较器的 OrderBy 和 ThenBy 方法对产品名称先按定价排序,然后执行不区分大小写的降序排序。
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim products As DataTable = ds.Tables("Product")
Dim query As IEnumerable(Of DataRow) = _
products.AsEnumerable().OrderBy(Function(product) product.Field(Of Decimal)("ListPrice")) _
.ThenBy(Function(product) product.Field(Of String)("Name"), _
New CaseInsensitiveComparer())
For Each product As DataRow In query
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", _
product.Field(Of Integer)("ProductID"), _
product.Field(Of String)("Name"), _
product.Field(Of Decimal)("ListPrice"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable products = ds.Tables["Product"];
IEnumerable<DataRow> query =
products.AsEnumerable().OrderBy(product => product.Field<Decimal>("ListPrice"))
.ThenBy(product => product.Field<string>("Name"),
new CaseInsensitiveComparer());
foreach (DataRow product in query)
{
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
product.Field<int>("ProductID"),
product.Field<string>("Name"),
product.Field<Decimal>("ListPrice"));
}