How to: Execute a Query that Returns an Anonymous Type

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.

To run the code in this example, you need to create a new Telerik Data Access Domain Model based on the Northwind database.

The following code is the LINQ example.

C#


    using ( EntitiesModel dbContext = new EntitiesModel() )
    {
       var query = from product in dbContext.Products
                   select new
                   {
                       ProductId = product.ProductID,
                       ProductName = product.ProductName
                   };
       foreach ( var item in query )
       {
           Console.WriteLine( "Product Id: {0} Product name: {1} ",
               item.ProductId, item.ProductName );
       }
    }

VB


    Using dbContext As New EntitiesModel()
     Dim query = From product In dbContext.Products
                 Select New With {Key .ProductId = product.ProductID, Key .ProductName = product.ProductName}
     For Each item In query
      Console.WriteLine("Product Id: {0} Product name: {1} ", item.ProductId, item.ProductName)
     Next item
    End Using

See Also