Esempi di sintassi di query basate sul metodo: ordinamento (LINQ to DataSet)Method-Based Query Syntax Examples: Ordering (LINQ to DataSet)

Negli esempi di questo argomento viene illustrato come usare i metodi OrderBy, Reverse e ThenBy per eseguire una query su DataSet e ordinare i risultati usando la sintassi delle query basate su metodo.The examples in this topic demonstrate how to use the OrderBy, Reverse, and ThenBy methods to query a DataSet and order the results using the method query syntax.

Il FillDataSet metodo usato in questi esempi è specificato nel durante il caricamento dei dati in un set di dati.The FillDataSet method used in these examples is specified in Loading Data Into a DataSet.

Negli esempi di questo argomento vengono usate le tabelle Contact, Address, Product, SalesOrderHeader e SalesOrderDetail del database di esempio AdventureWorks.The examples in this topic use the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

Gli esempi in questo argomento usano seguenti using / Imports istruzioni:The examples in this topic use the following using/Imports statements:

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

Per ulteriori informazioni, vedere procedura: creare un LINQ to DataSet progetto In Visual Studio.For more information, see How to: Create a LINQ to DataSet Project In Visual Studio.

OrderByOrderBy

EsempioExample

In questo esempio viene usato il metodo OrderBy con un operatore di confronto personalizzato per ordinare i cognomi senza distinzione tra maiuscole e minuscole.This example uses the OrderBy method with a custom comparer to do a case-insensitive sort of last names.

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

ReverseReverse

EsempioExample

In questo esempio viene usato il metodo Reverse per creare un elenco di ordini in cui il valore di OrderDate è precedente al 20 febbraio 2002.This example uses the Reverse method to create a list of orders where OrderDate is earlier than Feb 20, 2002.

// 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

ThenByThenBy

EsempioExample

In questo esempio vengono usati i metodi OrderBy e ThenBy con un operatore di confronto personalizzato per ordinare prima in base al prezzo di listino e quindi applicare l'ordinamento discendente e senza distinzione tra maiuscole e minuscole ai nomi di prodotto.This example uses OrderBy and ThenBy methods with a custom comparer to first sort by list price, and then perform a case-insensitive descending sort of the product names.

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

Vedere ancheSee Also

Caricamento di dati in un oggetto DataSetLoading Data Into a DataSet
Esempi di LINQ to DataSetLINQ to DataSet Examples
Cenni preliminari sugli operatori di query standardStandard Query Operators Overview