Export (0) Print
Expand All

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:
property String^ GroupBy {
	String^ get ();
	void set (String^ value);
}
<asp:LinqDataSource GroupBy="String" />

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 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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft