Data Access has been discontinued. Please refer to this page for more information.

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 a fluent model based on the Northwind database.

The following code is the LINQ example.

using ( FluentModel dbContext = new FluentModel() )
{
   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 );
   }
}
Using dbContext As New FluentModel()
 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