Share via


查詢運算式語法範例:排序 (LINQ to DataSet)

此主題中的範例將示範如何使用 OrderByOrderByDescendingReverseThenByDescending 方法並搭配查詢運算式語法來查詢 DataSet 以及排序結果。

將資料載入 DataSet 中指定了這些範例使用的 FillDataSet 方法。

此主題中的範例將使用 AdventureWorks 範例資料庫中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 資料表。

此主題中的範例使用下列 using/Imports 陳述式:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
Option Explicit On

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

如需詳細資訊,請參閱操作說明:在 Visual Studio 中建立 LINQ to DataSet 專案

OrderBy

範例

這則範例會使用 OrderBy 來傳回依據姓氏排序的連絡人清單。

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable contacts = ds.Tables["Contact"];

IEnumerable<DataRow> query =
    from contact in contacts.AsEnumerable()
    orderby contact.Field<string>("LastName")
    select contact;

Console.WriteLine("The sorted list of last names:");
foreach (DataRow contact in query)
{
    Console.WriteLine(contact.Field<string>("LastName"));
}
' 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 = _
    From contact In contacts.AsEnumerable() _
    Select contact _
    Order By contact.Field(Of String)("LastName")

Console.WriteLine("The sorted list of last names:")
For Each contact In query
    Console.WriteLine(contact.Field(Of String)("LastName"))
Next

範例

這則範例會使用 OrderBy 來依據姓氏的長度排序連絡人清單。

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable contacts = ds.Tables["Contact"];

IEnumerable<DataRow> query =
    from contact in contacts.AsEnumerable()
    orderby contact.Field<string>("LastName").Length
    select contact;

Console.WriteLine("The sorted list of last names (by length):");
foreach (DataRow contact in query)
{
    Console.WriteLine(contact.Field<string>("LastName"));
}
' 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 = _
    From contact In contacts.AsEnumerable() _
    Select contact _
    Order By contact.Field(Of String)("LastName").Length

Console.WriteLine("The sorted list of last names (by length):")
For Each contact In query
    Console.WriteLine(contact.Field(Of String)("LastName"))
Next

OrderByDescending

範例

這則範例會使用 orderby… descending (Order By … Descending,相當於 OrderByDescending 方法),從最高到最低排序價格清單。

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

IEnumerable<Decimal> query =
    from product in products.AsEnumerable()
    orderby product.Field<Decimal>("ListPrice") descending
    select product.Field<Decimal>("ListPrice");

Console.WriteLine("The list price from highest to lowest:");
foreach (Decimal product in query)
{
    Console.WriteLine(product);
}
' 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 = _
    From product In products.AsEnumerable() _
    Select product _
    Order By product.Field(Of Decimal)("ListPrice") Descending

Console.WriteLine("The list price From highest to lowest:")

For Each product In query
    Console.WriteLine(product.Field(Of Decimal)("ListPrice"))
Next

Reverse

範例

這則範例會使用 Reverse 來建立 OrderDate 早於 2002 年 2 月 20 日之訂單的清單。

// 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"));
}
' 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

ThenByDescending

範例

這則範例會使用 OrderBy… Descending (相當於 ThenByDescending 方法) 來排序產品的清單 (先依據名稱,然後再依據標價,從最高到最低)。

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

IEnumerable<DataRow> query =
    from product in products.AsEnumerable()
    orderby product.Field<string>("Name"),
        product.Field<Decimal>("ListPrice") descending
    select product;

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"));
}
' 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 = _
    From product In products.AsEnumerable() _
    Order By product.Field(Of String)("Name"), _
            product.Field(Of Decimal)("ListPrice") Descending _
    Select product

For Each product In query
    Console.Write("Product ID: " & product.Field(Of Integer)("ProductID"))
    Console.Write(" Product Name: " & product.Field(Of String)("Name"))
    Console.WriteLine(" List Price: " & product.Field(Of Decimal)("ListPrice"))
Next

另請參閱