GroupBy Property

LinqDataSource.GroupBy Property


Gets or sets a value that specifies which properties are used for grouping the retrieved data.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public string GroupBy { get; set; }

Property Value

Type: System.String

A string that is used to create the Group By clause.

You use the GroupBy property to specify which properties are used for consolidating data records that have the same values. For example, if you set the GroupBy property to Name, all the records in the query that have the same Name property value are returned as a single consolidated record.

You can assign more than one property to the GroupBy property by enclosing all the properties in the new function and separating each property by using a comma. For example, to group by the properties Name and then Category, set the GroupBy property to new(Name, Category).

The values in the property that are used for grouping are returned through a generated property named Key. You include the Key property in the Select property to retrieve the grouped values. You can set the Key property to an alias by using the As keyword, but you are not required to use an alias. For example, you might set the GroupBy property to a property named Category. You can retrieve the consolidated values from the Category property by setting the Select property to new(Key As ProductCategory).

You can access the individual records in a grouping by including the It property in the Select property. The It property contains a collection of records that share a value in the grouped property. You can iterate over the It property to retrieve the individual records.

The GroupBy property is often used with aggregation methods. You can use the following aggregate methods:

  • Count()

  • Average(column)

  • Sum(column)

  • Max(column)

  • Min(column)

  • Where(condition)

  • Any()

  • All(condition)

For more information, see NIB: LinqDataSource Web Server Control Overview and How to: Group and Aggregate Data Using the LinqDataSource Control.

The following example shows a LinqDataSource control that groups the returned data by a property named Category. It returns the shared values and calculates the average price for the grouped records.

The following example shows a LinqDataSource control that is configured to group by two columns. The Key property references an object that has two properties, ProductCategory and Color. The object represented by It is renamed Products. The renamed Products object contains a collection of the individual records in a grouping, and each instance contains all the columns from the Products table.

The following example shows two ListView controls for displaying the data from the LinqDataSource control in the previous example. One ListView control displays the grouped data and the other ListView control displays the individual names of products that belong to that group. The nested data-bound control's DataSource property is set to Products, which is the alias for the It object.

.NET Framework
Available since 3.5
Return to top
© 2015 Microsoft