共用方式為


HOW TO:執行傳回匿名型別的查詢 (Entity Framework)

本主題提供的範例示範如何執行傳回匿名型別之執行個體集合 (例如集合、資料列和參考) 的查詢。在這些範例中,查詢的結果是資料列型別的集合。如需詳細資訊,請參閱型別系統 (Entity 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 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);
    }
}

以下是 實體 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());
    }
}

另請參閱

工作

HOW TO:執行傳回實體類型的查詢 (Entity Framework)
HOW TO:執行傳回基本型別的查詢 (Entity Framework)
HOW TO:執行參數化查詢 (Entity Framework)

概念

查詢產生器方法 (Entity Framework)

其他資源

查詢 Entity Data Model (Entity Framework 工作)