Udostępnij za pośrednictwem


How to: Execute a Query that Returns Anonymous Type Objects

This topic provides examples of how to execute queries that return a collection of instances of an anonymous type. An anonymous type is a type that is not defined in the conceptual model. For example, the following Entity SQL query returns a collection of anonymous types that contain rows with two columns (an integer for the ProductID and a string for the Name): "SELECT p.ProductID, p.Name FROM AdventureWorksEntities.Products as p". The following example returns a collection of Products, and the Product type is defined in the coneptual model: "SELECT p FROM AdventureWorksEntities.Products as p".

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 example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

The following is the LINQ to Entities example.

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    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 context = new AdventureWorksEntities())
{
    var query =
        from product in context.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 context As New AdventureWorksEntities()
    Dim myQuery As String = "SELECT p.ProductID, p.Name FROM AdventureWorksEntities.Products as p"
    For Each rec As DbDataRecord In New ObjectQuery(Of DbDataRecord)(myQuery, context)
        Console.WriteLine("ID {0}; Name {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string myQuery = @"SELECT p.ProductID, p.Name FROM 
        AdventureWorksEntities.Products as p";

    foreach (DbDataRecord rec in
        new ObjectQuery<DbDataRecord>(myQuery, context))
    {
        Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
    }
}

The following is the query builder method example.

Using context As New AdventureWorksEntities()
    ' Use the Select method to define the projection. 
    Dim query As ObjectQuery(Of DbDataRecord) = context.Products.Select("it.ProductID, it.Name")

    ' Iterate through the collection of data rows. 
    For Each rec As DbDataRecord In query
        Console.WriteLine("ID {0}; Name {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Use the Select method to define the projection.
    ObjectQuery<DbDataRecord> query =
        context.Products.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]);
    }
}

See Also

Tasks

How to: Execute a Query that Returns Entity Type Objects
How to: Execute a Query that Returns a Collection of Primitive Types
How to: Execute a Parameterized Query

Concepts

Query Builder Methods