How to: Group Data
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This topic shows how to group query results. The example returns a set of nested data records that contain the Product.ProductName column, grouped and sorted alphabetically by the first letter of Product.ProductName.
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.
using ( EntitiesModel dbContext = new EntitiesModel() )
{
var query = ( from product in dbContext.Products
group product by product.ProductName.Substring( 0, 1 ) into productGroup
select new
{
FirstLetter = productGroup.Key,
Names = productGroup
} ).OrderBy( letter => letter.FirstLetter );
foreach ( var product in query )
{
Console.WriteLine( "Product names that start with the letter '{0}':",
product.FirstLetter );
foreach ( var name in product.Names )
{
Console.WriteLine( name.ProductName );
}
}
}
Using dbContext As New EntitiesModel()
Dim query = (
From product In dbContext.Products
Group product By GroupKey = product.ProductName.Substring(0, 1) Into productGroup = _
Group Select New With _
{ _
Key .FirstLetter = GroupKey, _
Key .Names = productGroup _
}).OrderBy(Function(letter) letter.FirstLetter)
For Each product In query
Console.WriteLine("Product names that start with the letter '{0}':", product.FirstLetter)
For Each name In product.Names
Console.WriteLine(name.ProductName)
Next name
Next product
End Using