查詢運算式語法範例:排序 (LINQ to Entities)
本主題中的範例將示範如何使用 OrderBy 和 OrderByDescending 方法,透過查詢運算式語法來查詢 AdventureWorks Sales Model。 這些範例中使用的 AdventureWorks Sales Model 是從 AdventureWorks 範例資料庫中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 資料表所建立。
此主題中的範例將使用下列 using/Imports 陳述式:
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;
如需詳細資訊,請參閱 HOW TO:在 Visual Studio 中建立 LINQ to Entities 專案。
OrderBy
範例
下列範例會使用 OrderBy 來傳回依據姓氏排序的連絡人清單。
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);
}
}
範例
下列範例會使用 OrderBy 來依據姓氏的長度排序連絡人清單。
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
範例
下列範例會使用 orderby… descending (Visual Basic 中的 Order By … Descending,相當於 OrderByDescending 方法),從最高到最低排序價格清單。
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
範例
下列範例會使用 OrderBy 和 ThenBy 來傳回先依據姓氏再依據名字排序的連絡人清單。
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
範例
下列範例會使用 OrderBy… Descending (相當於 ThenByDescending 方法) 來排序產品的清單 (先依據名稱,再依據標價,從最高到最低排序)。
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);
}
}