Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Execute a Query that Returns an Anonymous Type
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Querying a Data Model > How to: Execute a Query that Returns an Anonymous Type

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

In some cases we need only half of the information that we retrieve from the database. For those cases we need to either create a class with the needed fields or just use an anonymous class. Here is an example:

VB .NET Copy Code
scope.Transaction.Begin()
Dim result = (From c In scope.Extent(Of RentalRate)() _
              Select New With {Key c.Category, Key c.RentalRateID}).ToList()
RadGrid1.DataSource = result
RadGrid1.DataBind()
scope.Transaction.Commit()
scope.Dispose()
C# Copy Code
scope.Transaction.Begin();
var result = (from c in scope.Extent<RentalRate>()
             select
new { c.Category,c.RentalRateID }).ToList();
RadGrid1.DataSource = result;
RadGrid1.DataBind();
scope.Transaction.Commit();
scope.Dispose();

In this case we only need the fields "Category" and "RentalRateID" so instead of "select c" in the end of the linq query we write "select new()". In the brackets after new we can specify which fields to be selected. In the case above the result object will be represented as a list of class objects which have 2 fields, one for Category and one for RentalRateID. When we bind it to the radgrid, the grid will only display those 2 fields instead of all the fields in the RentalRate table.