HOW TO:排序資料 (Entity Framework)
本主題將說明如何排序查詢結果。此範例會傳回依照 Contact.LastName 第一個字母的英文順序排序的 Contact 物件集合。此外,也會顯示使用下列 實體架構 查詢技術的相同範例:
LINQ to Entities
使用 ObjectQuery<T> 的 Entity SQL
ObjectQuery<T> 的查詢產生器方法
本主題的範例是根據 Adventure Works Sales Model。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 來定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用 Entity Data Model 精靈 (Entity Framework)。
範例
以下是 LINQ 到實體 範例。
Using context As AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Define a query that returns a list
' of Contact objects sorted by last name.
Dim sortedNames = _
From n In context.Contact _
Order By n.LastName _
Select n
Console.WriteLine("The sorted list of last names:")
For Each name As Contact In sortedNames
Console.WriteLine(name.LastName)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Define a query that returns a list
// of Contact objects sorted by last name.
var sortedNames =
from n in context.Contact
orderby n.LastName
select n;
Console.WriteLine("The sorted list of last names:");
foreach (Contact name in sortedNames)
{
Console.WriteLine(name.LastName);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
以下是 實體 SQL 範例。
' Define the Entity SQL query string that returns
' Contact objects sorted by last name.
Dim queryString As String = "SELECT VALUE n FROM Contact AS n " _
+ "Order By n.LastName"
Using context As AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Define an ObjectQuery that returns a collection
' of Contact objects sorted by last name.
Dim query As ObjectQuery(Of Contact) = _
New ObjectQuery(Of Contact)(queryString, context)
Console.WriteLine("The sorted list of last names:")
For Each name As Contact _
In query.Execute(MergeOption.AppendOnly)
Console.WriteLine(name.LastName)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
// Define the Entity SQL query string that returns
// Contact objects sorted by last name.
string queryString = @"SELECT VALUE n FROM Contact AS n
Order By n.LastName";
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Define an ObjectQuery that returns a collection
// of Contact objects sorted by last name.
ObjectQuery<Contact> query =
new ObjectQuery<Contact>(queryString, context);
Console.WriteLine("The sorted list of last names:");
foreach (Contact name in query.Execute(MergeOption.AppendOnly))
{
Console.WriteLine(name.LastName);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
以下是查詢產生器方法範例。
Using context As AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Define an ObjectQuery that returns a collection
' of Contact objects sorted by last name.
Dim query As ObjectQuery(Of Contact) = _
context.Contact.OrderBy("it.LastName")
Console.WriteLine("The sorted list of last names:")
For Each name As Contact _
In query.Execute(MergeOption.AppendOnly)
Console.WriteLine(name.LastName)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Define an ObjectQuery that returns a collection
// of Contact objects sorted by last name.
ObjectQuery<Contact> query =
context.Contact.OrderBy("it.LastName");
Console.WriteLine("The sorted list of last names:");
foreach (Contact name in query.Execute(MergeOption.AppendOnly))
{
Console.WriteLine(name.LastName);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}