Beispiele für die Abfrageausdruckssyntax: Sortieren (LINQ to Entities)
In den Beispielen in diesem Thema wird gezeigt, wie Sie mithilfe der OrderBy-Methode und der OrderByDescending-Methode das AdventureWorks Sales-Modell unter Verwendung der Abfrageausdruckssyntax abfragen. Für das in den Beispielen verwendete "AdventureWorks Sales"-Modell wurde auf die Tabellen Contact, Address, Product, SalesOrderHeader und SalesOrderDetail der "AdventureWorks"-Beispieldatenbank zurückgegriffen.
In den Beispielen in diesem Thema werden die folgenden using/Imports-Anweisungen verwendet:
Option Explicit On
Option Strict On
Imports L2EExamplesVB.AdventureWorksModel
Imports System.Data.Objects
Imports System.Globalization
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using AdventureWorksModel;
using System.Globalization;
Weitere Informationen finden Sie unter Gewusst wie: Erstellen eines LINQ to Entities-Projekts in Visual Studio.
OrderBy
Beispiel
Im folgenden Beispiel wird OrderBy verwendet, um eine nach Nachnamen sortierte Liste der Kontakte zurückzugeben.
Using AWEntities As New AdventureWorksEntities
Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact
Dim sortedContacts = _
From contact In contacts _
Order By contact.LastName _
Select contact
Console.WriteLine("The sorted list of last names:")
For Each n As Contact In sortedContacts
Console.WriteLine(n.LastName)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Contact> contacts = AWEntities.Contact;
IQueryable<Contact> sortedNames =
from n in contacts
orderby n.LastName
select n;
Console.WriteLine("The sorted list of last names:");
foreach (Contact n in sortedNames)
{
Console.WriteLine(n.LastName);
}
}
Beispiel
Im folgenden Beispiel wird OrderBy verwendet, um eine Liste von Kontakten nach der Länge der Nachnamen zu sortieren.
Using AWEntities As New AdventureWorksEntities
Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact
Dim sortedNames = _
From n In contacts _
Order By n.LastName.Length _
Select n
Console.WriteLine("The sorted list of last names (by length):")
For Each n As Contact In sortedNames
Console.WriteLine(n.LastName)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Contact> contacts = AWEntities.Contact;
IQueryable<Contact> sortedNames =
from n in contacts
orderby n.LastName.Length
select n;
Console.WriteLine("The sorted list of last names (by length):");
foreach (Contact n in sortedNames)
{
Console.WriteLine(n.LastName);
}
}
OrderByDescending
Beispiel
Im folgenden Beispiel wird orderby… descending (Order By … Descending in Visual Basic), das Äquivalent zur OrderByDescending-Methode, verwendet, um die Preisliste in absteigender Reihenfolge (höchster Preis zuerst, niedrigster zuletzt) zu sortieren.
Using AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
Dim sortedPrices = _
From product In products _
Order By product.ListPrice Descending _
Select product.ListPrice
Console.WriteLine("The list price from highest to lowest:")
For Each price As Decimal In sortedPrices
Console.WriteLine(price)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
IQueryable<Decimal> sortedPrices =
from p in products
orderby p.ListPrice descending
select p.ListPrice;
Console.WriteLine("The list price from highest to lowest:");
foreach (Decimal price in sortedPrices)
{
Console.WriteLine(price);
}
}
ThenBy
Beispiel
Im folgenden Beispiel werden OrderBy und ThenBy verwendet, um eine zuerst nach Nachnamen und dann nach Vornamen sortierte Liste der Kontakte zurückzugeben.
Using AWEntities As New AdventureWorksEntities
Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact
Dim sortedContacts = _
From contact In contacts _
Order By contact.LastName, contact.FirstName _
Select contact
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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Contact> contacts = AWEntities.Contact;
IQueryable<Contact> sortedContacts =
from contact in contacts
orderby contact.LastName, contact.FirstName
select contact;
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);
}
}
ThenByDescending
Beispiel
Im folgenden Beispiel wird OrderBy… Descending, das Äquivalent zur ThenByDescending-Methode, verwendet, um eine Liste von Produkten zuerst nach dem Namen und dann absteigend nach dem Listenpreis (höchster Preis zuerst, niedrigster Preis zuletzt) zu sortieren.
Using AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
Dim query As IQueryable(Of Product) = _
From product In products _
Order By product.Name, product.ListPrice Descending _
Select product
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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
IQueryable<Product> query =
from product in products
orderby product.Name, product.ListPrice descending
select product;
foreach (Product product in query)
{
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
product.ProductID,
product.Name,
product.ListPrice);
}
}