如何:执行返回匿名类型的查询(实体框架)
本主题提供的示例介绍如何执行返回匿名类型(如集合、行和引用)的实例集合的查询。这些示例中查询的结果是行类型集合。有关更多信息,请参见类型系统 (Entity SQL)。将使用以下每种 实体框架 查询技术演示同一示例:
LINQ to Entities
Entity SQL with ObjectQuery<T>
ObjectQuery <T> 的查询生成器方法
本主题中的示例基于 Adventure Works 销售模型。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用实体框架。为此,请完成如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架) 中的过程。也可以使用实体数据模型向导定义 AdventureWorks 销售模型。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)。
示例
以下是 LINQ to Entities 示例。
Using AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
Dim query = _
From product In products _
Select New With _
{ _
.ProductId = product.ProductID, _
.ProductName = product.Name _
}
Console.WriteLine("Product Info:")
For Each productInfo In query
Console.WriteLine("Product Id: {0} Product name: {1} ", _
productInfo.ProductId, productInfo.ProductName)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
var query =
from product in products
select new
{
ProductId = product.ProductID,
ProductName = product.Name
};
Console.WriteLine("Product Info:");
foreach (var productInfo in query)
{
Console.WriteLine("Product Id: {0} Product name: {1} ",
productInfo.ProductId, productInfo.ProductName);
}
}
以下是 Entity SQL 示例。
Using advWorksContext As AdventureWorksEntities = New AdventureWorksEntities
Dim commandText As String = "SELECT p.ProductID, p.Name FROM " & _
"AdventureWorksEntities.Product as p"
Try
Dim query As New ObjectQuery(Of DbDataRecord)(commandText, advWorksContext)
For Each result As DbDataRecord In query
Console.WriteLine("ID {0} Name {1}", result.Item(0), result.Item(1))
Next
Catch ex As EntityException
Console.WriteLine(ex.ToString())
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
string myQuery = @"SELECT p.ProductID, p.Name FROM
AdventureWorksEntities.Product as p";
try
{
foreach (DbDataRecord rec in
new ObjectQuery<DbDataRecord>(myQuery, advWorksContext))
{
Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
}
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
以下是查询生成器方法示例。
Using advWorksContext As AdventureWorksEntities = _
New AdventureWorksEntities
Try
' Use the Select method to define the projection.
Dim query As ObjectQuery(Of DbDataRecord) = _
advWorksContext.Product.Select("it.ProductID, it.Name")
' Iterate through the collection of data rows.
For Each result As DbDataRecord In query
Console.WriteLine("ID{0}: Name {1}", _
result.Item(0), result.Item(1))
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Use the Select method to define the projection.
ObjectQuery<DbDataRecord> query =
advWorksContext.Product.Select("it.ProductID, it.Name");
// Iterate through the collection of data rows.
foreach (DbDataRecord rec in query)
{
Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
另请参见
任务
如何:执行返回实体类型的查询(实体框架)
如何:执行返回基元类型的查询(实体框架)
如何:执行参数化查询(实体框架)