Gets or sets a value that specifies which properties are used for grouping the retrieved data.
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
You use the property to specify which properties are used for consolidating data records that have the same values. For example, if you set the 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 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 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 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 property is often used with aggregation methods. You can use the following aggregate methods:
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.
<asp:LinqDataSource ContextTypeName="ExampleDataContext" TableName="Products" GroupBy="Category" Select="new(Key as ProductCategory, Average(Price) as AvePrice)" ID="LinqDataSource1" runat="server"> </asp:LinqDataSource> <asp:GridView AllowPaging="true" DataSourceID="LinqDataSource1" ID="GridView1" runat="server"> </asp:GridView>
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.
<asp:ListView DataSourceID="LinqDataSource1" ID="ListView1" runat="server"> <LayoutTemplate> <table id="Table1" style="background-color:Teal;color:White" runat="server" class="Layout"> <thead> <tr> <th><b>Product Category</b></th> <th><b>Color</b></th> <th><b>Highest Price</b></th> <th><b>Lowest Price</b></th> </tr> </thead> <tr runat="server" id="itemPlaceholder"> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%# Eval("key.ProductCategory") %></td> <td><%# Eval("key.Color") %></td> <td><%# Eval("MaxListPrice") %></td> <td><%# Eval("MinListPrice") %></td> </tr> <tr> <td colspan="4" style="width:100%;background-color:White;color:Black"> <asp:ListView DataSource='<%# Eval("Products") %>' runat="server" ID="ListView2"> <LayoutTemplate> <div runat="server" id="itemPlaceholder" /> </LayoutTemplate> <ItemTemplate> <%# Eval("ProductName") %><br /> </ItemTemplate> </asp:ListView> </td> </tr> </ItemTemplate> </asp:ListView>
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.