LinqDataSource.GroupBy 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指定用于对检索到的数据进行分组的属性。
public:
property System::String ^ GroupBy { System::String ^ get(); void set(System::String ^ value); };
public string GroupBy { get; set; }
member this.GroupBy : string with get, set
Public Property GroupBy As String
属性值
用于创建 Group By 子句的字符串。
示例
The following example shows a LinqDataSource control that groups the returned data by a property named Category
. 它返回共享值并计算分组记录的平均价格。
<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>
<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>
以下示例显示了 LinqDataSource 配置为按两列分组的控件。 该 Key
属性引用具有两个属性的对象, ProductCategory
以及 Color
。 所 It
表示的对象已重命名 Products
。 重命名 Products
的对象包含分组中各个记录的集合,每个实例都包含 Products 表中的所有列。
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Products"
GroupBy="new(ProductCategory, Color)"
Select="new(Key,
It As Products,
Max(ListPrice) As MaxListPrice,
Min(ListPrice) As MinListPrice)"
ID="LinqDataSource1"
runat="server">
</asp:LinqDataSource>
<asp:LinqDataSource
ContextTypeName="ExampleDataContext"
TableName="Products"
GroupBy="new(ProductCategory, Color)"
Select="new(Key,
It As Products,
Max(ListPrice) As MaxListPrice,
Min(ListPrice) As MinListPrice)"
ID="LinqDataSource1"
runat="server">
</asp:LinqDataSource>
以下示例演示了两 ListView 个控件,用于显示上一示例中控件中的数据 LinqDataSource 。 一个控件显示分组数据,另一 ListView 个 ListView 控件显示属于该组的产品的各个名称。 嵌套数据绑定控件 DataSource 的属性设置为 Products
该对象别名 It
。
<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>
<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>
注解
可以使用该 GroupBy 属性指定用于合并具有相同值的数据记录的属性。 例如,如果将属性设置为 GroupBy 该属性 Name
,则查询中具有相同 Name
属性值的所有记录将作为单个合并记录返回。
可以通过将函数中的所有属性括起来new
并使用逗号分隔每个属性来为属性分配多个属性GroupBy。 例如,若要按属性Name
分组,然后将Category
属性new(Name, Category)
设置为 GroupBy 。
The values in the property that are used for grouping are returned through a generated property named Key
. 在属性中包含Select用于Key
检索分组值的属性。 可以使用关键字将 Key
属性设置为别名,但不需要使用别名 As
。 For example, you might set the GroupBy property to a property named Category
. 通过将属性new(Key As ProductCategory)
设置为 Select ., 可以从属性中检索合并值Category
。
可以通过在 It
属性中包含属性 Select 来访问分组中的单个记录。 该 It
属性包含共享分组属性中的值的记录集合。 可以循环访问 It
属性以检索各个记录。
该 GroupBy 属性通常用于聚合方法。 可以使用以下聚合方法:
Count()
Average(
column)
Sum(
column)
Max(
column)
Min(
column)
Where(
条件)
Any()
All(
条件)
有关详细信息,请参阅 LinqDataSource Web 服务器控件概述 和 操作方法:使用 LinqDataSource 控件对数据进行分组和聚合。