LinqDataSource.GroupBy 属性

定义

获取或设置一个值,该值指定用于对检索到的数据进行分组的属性。

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 子句的字符串。

示例

以下示例演示一个 LinqDataSource 控件,该控件按名为 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引用具有两个属性和 Color的对象ProductCategory。 表示的对象 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 属性指定用于合并具有相同值的数据记录的属性。 例如,如果将 属性设置为 GroupByName,则查询中具有相同 Name 属性值的所有记录将作为单个合并记录返回。

可以通过将函数中的所有属性 GroupBy 括起来 new 并使用逗号分隔每个属性来为属性分配多个属性。 例如,若要按属性 Name 分组,然后将 Category属性 GroupBy 设置为 new(Name, Category)

属性中用于分组的值通过生成的名为 Key返回。 Key在 属性中包含 Select 属性以检索分组值。 可以使用 关键字将 Key 属性设置为别名 As ,但不需要使用别名。 例如,可以将 属性 GroupBy 设置为名为 的属性 Category。 可以通过将 属性设置为 Selectnew(Key As ProductCategory)来检索Category属性中的合并值。

可以通过在 属性中包含 属性来访问分组 It 中的 Select 单个记录。 属性 It 包含共享分组属性中的值的记录集合。 可以循环访问 It 属性以检索单个记录。

属性 GroupBy 通常与聚合方法一起使用。 可以使用以下聚合方法:

  • Count()

  • Average(column)

  • Sum(column)

  • Max(column)

  • Min(column)

  • Where(condition)

  • Any()

  • All(condition)

有关详细信息,请参阅 LinqDataSource Web 服务器控件概述如何:使用 LinqDataSource 控件对数据进行分组和聚合

适用于