HOW TO:執行傳回實體類型的查詢 (Entity Framework)
本主題提供的範例將說明如何執行 實體 SQL 查詢來傳回實體類型執行個體的集合。然後,它會逐一查看產品的集合,並顯示每一項產品的名稱和識別碼。也會顯示使用下列每一個 實體架構 查詢技術的相同範例:
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 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);
}
}
以下是 實體 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());
}
}
另請參閱
工作
HOW TO:執行傳回匿名型別的查詢 (Entity Framework)
HOW TO:執行傳回基本型別的查詢 (Entity Framework)
HOW TO:執行參數化查詢 (Entity Framework)