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 属性引用具有两个属性的对象,ProductCategoryColor。 由 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,请将 GroupBy 属性设置为 new(Name, Category)

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

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

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

  • Count()

  • Average( )

  • Sum( )

  • Max( )

  • Min( )

  • Where( 条件)

  • Any()

  • All( 条件)

有关详细信息,请参阅 LinqDataSource Web Server Control OverviewHow to: Group and Aggregate Data Using the LinqDataSource Control.

适用于