Ejemplos de sintaxis de consulta basada en métodos: OrdenaciónMethod-Based Query Syntax Examples: Ordering

Los ejemplos de este tema muestran cómo usar el ThenBy método para consultar el modelo AdventureWorks Sales utilizando sintaxis de consulta basada en métodos.The examples in this topic demonstrate how to use the ThenBy method to query the AdventureWorks Sales Model using method-based query syntax. El modelo AdventureWorks Sales que se usa en estos ejemplos se crea a partir de las tablas Contact, Address, Product, SalesOrderHeader y SalesOrderDetail en la base de datos de ejemplo de AdventureWorks.The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

Los ejemplos de este tema usan los siguientes using / Imports instrucciones:The examples in this topic use the following using/Imports statements:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;
Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization

ThenByThenBy

EjemploExample

El siguiente ejemplo con la sintaxis de consulta basada en métodos usa OrderBy y ThenBy para devolver una lista de contactos ordenados primero por el apellido y después por el nombre:The following example in method-based query syntax uses OrderBy and ThenBy to return a list of contacts ordered by last name and then by first name.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Contact> sortedContacts = context.Contacts
        .OrderBy(c => c.LastName)
        .ThenBy(c => c.FirstName);

    Console.WriteLine("The list of contacts sorted by last name then by first name:");
    foreach (Contact sortedContact in sortedContacts)
    {
        Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName);
    }
}
Using context As New AdventureWorksEntities

    Dim sortedContacts = context.Contacts _
    .OrderBy(Function(c) c.LastName) _
    .ThenBy(Function(c) c.FirstName) _
    .Select(Function(c) c)

    Console.WriteLine("The list of contacts sorted by last name then by first name:")
    For Each sortedContact As Contact In sortedContacts
        Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName)
    Next
End Using

ThenByDescendingThenByDescending

EjemploExample

En el ejemplo siguiente se utilizan los métodos OrderBy y ThenByDescending para ordenar primero por precio de venta y después realizar una ordenación en orden descendente de los nombres de producto.The following example uses the OrderBy and ThenByDescending methods to first sort by list price, and then perform a descending sort of the product names.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IOrderedQueryable<Product> query = context.Products
        .OrderBy(product => product.ListPrice)
        .ThenByDescending(product => product.Name);

    foreach (Product product in query)
    {
        Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
            product.ProductID,
            product.Name,
            product.ListPrice);
    }
}
Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim query As IOrderedQueryable(Of Product) = products _
    .OrderBy(Function(prod As Product) prod.ListPrice) _
    .ThenByDescending(Function(prod As Product) prod.Name)

    For Each prod As Product In query
        Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", _
            prod.ProductID, _
            prod.Name, _
            prod.ListPrice)
    Next
End Using

Vea tambiénSee also