Udostępnij za pośrednictwem


How to: Execute a Query that Returns an Anonymous Type (Entity Framework)

This topic provides examples of how to execute queries that return a collection of instances of an anonymous type such as: collection, row, and reference. The result of the query in these examples is a collection of row types. For more information, see Type System (Entity SQL). The same example is shown using each of the following Entity Framework query technologies:

  • LINQ to Entities

  • Entity SQL with ObjectQuery<T>

  • Query builder methods of ObjectQuery<T>

The examples in this topic are based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework). You can also use the Entity Data Model Wizard to define the AdventureWorks Sales Model. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework).

Example

The following is the LINQ to Entities example.

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);
    }
}

The following is the Entity SQL example.

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());
    }
}

The following is the query builder method example.

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());
    }
}

See Also

Tasks

How to: Execute a Query that Returns an Entity Type (Entity Framework)
How to: Execute a Query that Returns a Primitive Type (Entity Framework)
How to: Execute a Parameterized Query (Entity Framework)

Concepts

Query Builder Methods (Entity Framework)

Other Resources

Querying an Entity Data Model (Entity Framework Tasks)