Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Aggregate Data
See Also
Programmer's Guide > Feature Reference > API > LINQ Guide > How to: Aggregate Data

Glossary Item Box

An aggregation operation computes a single value from a collection of values. An example of an aggregation operation is the calculating of the average daily temperature from a month's worth of daily temperature values. This topic shows how to group orders' details by CustomerID and get the average unit price for each CustomerID.

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

The following code is the LINQ example.

C# Copy Code
using ( EntitiesModel dbContext = new EntitiesModel() )
{
   var query = from orderDetail
in dbContext.OrderDetails
               group orderDetail by orderDetail.Order.CustomerID into o
               select
new
               {
                   
CustomerID = o.Key,
                   AverageUnitPrice = o.Average( od => od.UnitPrice )
               };
   
foreach ( var item in query )
   {
       Console.WriteLine(
"CustomerID = {0} \t Average UnitPrice = {1}",
           item.CustomerID, item.AverageUnitPrice );
   }
}
VB.NET Copy Code
Using dbContext As New EntitiesModel()
 Dim query = From orderDetail In dbContext.OrderDetails
    Group orderDetail By orderDetail.Order.CustomerID Into o = Group
    Select New With {Key .CustomerID = CustomerID, Key .AverageUnitPrice = o.Average(Function(od) od.UnitPrice)}
 For Each item In query
  Console.WriteLine("CustomerID = {0} " & vbTab & " Average UnitPrice = {1}", item.CustomerID, item.AverageUnitPrice)
 Next item
End Using

See Also