如何:对数据进行排序(实体框架)
本主题介绍如何对查询结果进行排序。 本示例返回 Contact 对象的集合,其结果按 Contact.LastName 首字母的字母顺序排序。 下面使用以下实体框架 查询技术演示同一示例:
LINQ to Entities
Entity SQL 以及 ObjectQuery<T>
ObjectQuery<T> 的查询生成器方法
本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)或如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架)。
示例
以下是 LINQ to Entities 示例。
Using context As New AdventureWorksEntities()
' Define a query that returns a list
' of Contact objects sorted by last name.
Dim sortedNames = From n In context.Contacts _
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
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Define a query that returns a list
// of Contact objects sorted by last name.
var sortedNames =
from n in context.Contacts
orderby n.LastName
select n;
Console.WriteLine("The sorted list of last names:");
foreach (Contact name in sortedNames)
{
Console.WriteLine(name.LastName);
}
}
以下是 Entity SQL 示例。
' Define the Entity SQL query string that returns
' Contact objects sorted by last name.
Dim queryString As String = "SELECT VALUE contact FROM Contacts AS contact Order By contact.LastName"
Using context As New AdventureWorksEntities()
' Define an ObjectQuery that returns a collection
' of Contact objects sorted by last name.
Dim query As 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
End Using
// Define the Entity SQL query string that returns
// Contact objects sorted by last name.
string queryString = @"SELECT VALUE contact FROM Contacts AS contact
Order By contact.LastName";
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// 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);
}
}
以下是查询生成器方法示例。
Using context As New AdventureWorksEntities()
' Define an ObjectQuery that returns a collection
' of Contact objects sorted by last name.
Dim query As ObjectQuery(Of Contact) = context.Contacts.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
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Define an ObjectQuery that returns a collection
// of Contact objects sorted by last name.
ObjectQuery<Contact> query =
context.Contacts.OrderBy("it.LastName");
Console.WriteLine("The sorted list of last names:");
foreach (Contact name in query.Execute(MergeOption.AppendOnly))
{
Console.WriteLine(name.LastName);
}
}