如何:对两个查询的联合排序(实体框架)
本主题说明如何将两个查询的结果组合成一个结果集,然后对此结果集排序。 将使用以下每种实体框架 查询技术演示同一示例:
Entity SQL 以及 ObjectQuery<T>
ObjectQuery<T> 的查询生成器方法
ObjectQuery<T> 的查询生成器方法
本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)或如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架)。
示例
以下是 Entity SQL 示例。 若要对 Entity SQL 查询进行联合和排序,您必须使用嵌套。 在 Entity SQL 中,嵌套的查询必须括在括号中。
Using context As New AdventureWorksEntities()
Dim esqlQuery As String = "SELECT P2.Name, P2.ListPrice FROM ((SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice " & _
" FROM AdventureWorksEntities.Products as P1 where P1.Name like 'A%') union all" & _
" (SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice FROM AdventureWorksEntities.Products as P1" & _
" WHERE P1.Name like 'B%')) as P2 ORDER BY P2.Name"
For Each rec As DbDataRecord In New ObjectQuery(Of DbDataRecord)(esqlQuery, context)
Console.WriteLine("Name: {0}; ListPrice: {1}", rec(0), rec(1))
Next
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
String esqlQuery = @"SELECT P2.Name, P2.ListPrice
FROM ((SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice
FROM AdventureWorksEntities.Products as P1
where P1.Name like 'A%')
union all
(SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice
FROM AdventureWorksEntities.Products as P1
WHERE P1.Name like 'B%')
) as P2
ORDER BY P2.Name";
foreach (DbDataRecord rec in
new ObjectQuery<DbDataRecord>(esqlQuery, context))
{
Console.WriteLine("Name: {0}; ListPrice: {1}", rec[0], rec[1]);
}
}
这是查询生成器方法示例。
Using context As New AdventureWorksEntities()
Dim query As ObjectQuery(Of DbDataRecord) = _
context.Products.Select("it.Name, it.ProductID As Pid, it.ListPrice") _
.Where("it.Name LIKE 'A%'").Union(context.Products.Select("it.Name, it.ProductID As Pid, it.ListPrice") _
.Where("it.Name LIKE 'B%'")).Select("it.Name, it.ListPrice").OrderBy("it.Name")
For Each rec As DbDataRecord In query
Console.WriteLine("Name: {0}; ListPrice: {1}", rec(0), rec(1))
Next
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
ObjectQuery<DbDataRecord> query =
context.Products.Select("it.Name, it.ProductID As Pid, it.ListPrice")
.Where("it.Name LIKE 'A%'").Union(context.Products
.Select("it.Name, it.ProductID As Pid, it.ListPrice")
.Where("it.Name LIKE 'B%'")).Select("it.Name, it.ListPrice").OrderBy("it.Name");
foreach (DbDataRecord rec in query)
{
Console.WriteLine("Name: {0}; ListPrice: {1}", rec[0], rec[1]);
}
}
下面的示例演示如何使用 LINQ to Entities 对两个查询的联合进行排序。
Using context As New AdventureWorksEntities()
Dim query = (From a In context.Products Where a.Name.StartsWith("A") _
Select (New With {a.Name, .pid = a.ProductID, a.ListPrice})). _
Union( _
From b In context.Products Where b.Name.StartsWith("B") _
Select (New With {b.Name, .pid = b.ProductID, b.ListPrice})). _
Select(Function(c) New With {c.Name, c.ListPrice}).OrderBy(Function(d) d.Name)
For Each result In query
Console.WriteLine(result.Name)
Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
var query = (from a in context.Products
where a.Name.StartsWith("A")
select new { a.Name, pid = a.ProductID, a.ListPrice })
.Union
(from b in context.Products
where b.Name.StartsWith("B")
select new { b.Name, pid = b.ProductID, b.ListPrice })
.Select(c => new { c.Name, c.ListPrice }).OrderBy(d => d.Name);
foreach (var result in query)
{
Console.WriteLine(result.Name);
}
}