如何:执行返回实体类型的查询(实体框架)
本主题提供的示例说明如何执行一个 Entity SQL 查询,该查询返回属于某个实体类型的实例集合。然后,它循环访问 Products 集合并显示每个 Product(产品)的名称和 ID。将使用以下每种 实体框架 查询技术演示同一示例:
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 productsQuery = _
From product In products _
Select product
Console.WriteLine("Product Names:")
For Each product In productsQuery
Console.WriteLine(product.Name)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
IQueryable<Product> productsQuery = from product in products
select product;
Console.WriteLine("Product Names:");
foreach (var prod in productsQuery)
{
Console.WriteLine(prod.Name);
}
}
以下是 Entity SQL 示例。
Using advWorksContext As AdventureWorksEntities = New AdventureWorksEntities
Try
Dim queryString As String = "SELECT VALUE Product FROM AdventureWorksEntities.Product AS Product"
Dim query As New ObjectQuery(Of Product)(queryString, advWorksContext, MergeOption.NoTracking)
' Iterate through the collection of Product items.
For Each result As Product In query
Console.WriteLine("Product Name: {0} Product ID: {1}", _
result.Name, result.ProductID)
Next
Catch exception As EntityException
Console.WriteLine(exception.ToString)
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
string queryString =
@"SELECT VALUE Product FROM AdventureWorksEntities.Product AS Product";
ObjectQuery<Product> productQuery =
new ObjectQuery<Product>(queryString, advWorksContext, MergeOption.NoTracking);
// Iterate through the collection of Product items.
foreach (Product result in productQuery)
Console.WriteLine("Product Name: {0}; Product ID: {1}",
result.Name, result.ProductID);
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
以下是查询生成器方法示例。
Using advWorksContext As AdventureWorksEntities = _
New AdventureWorksEntities
Try
Dim query As ObjectQuery(Of Product) = _
advWorksContext.Product
' Iterate through the collection of Product items.
For Each result As Product In query
Console.WriteLine("Product Name:{0}Product ID: {1}", _
result.Name, result.ProductID)
Next
Catch exception As EntitySqlException
Console.WriteLine(exception.ToString)
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
ObjectQuery<Product> productQuery = advWorksContext.Product;
// Iterate through the collection of Product items.
foreach (Product result in productQuery)
Console.WriteLine("Product Name: {0}; Product ID: {1}",
result.Name, result.ProductID);
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
另请参见
任务
如何:执行返回匿名类型的查询(实体框架)
如何:执行返回基元类型的查询(实体框架)
如何:执行参数化查询(实体框架)