LinqDataSource.GroupBy 屬性

定義

取得或設定值,這個值指定用於對擷取的資料進行分組的屬性。Gets or sets a value that specifies which properties are used for grouping the retrieved data.

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

屬性值

String

用來建立 Group By 子句的字串。A string that is used to create the Group By clause.

範例

下列範例顯示的 LinqDataSource 控制項會依名為的屬性,將傳回的資料分組 CategoryThe 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>
<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 控制項設定為依兩個數據行分組。The following example shows a LinqDataSource control that is configured to group by two columns. Key屬性參考的物件具有兩個屬性: ProductCategoryColorThe Key property references an object that has two properties, ProductCategory and Color. 表示的物件 It 已重新命名 ProductsThe object represented by It is renamed Products. 重新命名的 Products 物件包含群組中個別記錄的集合,而且每個實例都包含 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.

<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 上一個範例中的控制項資料。The following example shows two ListView controls for displaying the data from the LinqDataSource control in the previous example. 其中一個 ListView 控制項會顯示群組資料,而另一個 ListView 控制項會顯示屬於該群組之產品的個別名稱。One ListView control displays the grouped data and the other ListView control displays the individual names of products that belong to that group. 嵌套資料繫結控制項的 DataSource 屬性會設定為,也 Products 就是物件的別名 ItThe 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>
<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 屬性來指定要使用哪些屬性來合併有相同值的資料記錄。You use the GroupBy property to specify which properties are used for consolidating data records that have the same values. 例如,如果您將屬性設定 GroupByName ,則查詢中所有具有相同 Name 屬性值的記錄都會以單一合併記錄傳回。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.

您可以藉由在函式 GroupBy 中包含所有屬性 new ,並使用逗號分隔每個屬性,以將多個屬性指派給屬性。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. 例如,若要依屬性分組, Name 然後 CategoryGroupBy 屬性設定為 new(Name, Category)For example, to group by the properties Name and then Category, set the GroupBy property to new(Name, Category).

屬性中用於分組的值會透過名為的產生屬性來傳回 KeyThe values in the property that are used for grouping are returned through a generated property named Key. 您會 Key 在屬性中包含 Select 屬性,以抓取群組值。You include the Key property in the Select property to retrieve the grouped values. 您可以 Key 使用關鍵字將屬性設為別名 As ,但不需要使用別名。You can set the Key property to an alias by using the As keyword, but you are not required to use an alias. 例如,您可以將屬性設定 GroupBy 為名為的屬性 CategoryFor example, you might set the GroupBy property to a property named Category. 您可以藉 Category 由將屬性設定為,從屬性取出合併的值 Select new(Key As ProductCategory)You can retrieve the consolidated values from the Category property by setting the Select property to new(Key As ProductCategory).

您可以在屬性中包含屬性,以存取群組中的個別記錄 It SelectYou can access the individual records in a grouping by including the It property in the Select property. It屬性包含共用群組屬性值的記錄集合。The It property contains a collection of records that share a value in the grouped property. 您可以反復查看 It 屬性以取得個別記錄。You can iterate over the It property to retrieve the individual records.

GroupBy屬性通常與匯總方法搭配使用。The GroupBy property is often used with aggregation methods. 您可以使用下列匯總方法:You can use the following aggregate methods:

  • Count()

  • Average( column )Average( column )

  • Sum( column )Sum( column )

  • Max( column )Max( column )

  • Min( column )Min( column )

  • Where(條件)Where( condition )

  • Any()

  • All(條件)All( condition )

如需詳細資訊,請參閱 Linqdatasource Web 服務器控制項總覽 和 how To:使用 Linqdatasource 控制項來群組和匯總資料For more information, see LinqDataSource Web Server Control Overview and How to: Group and Aggregate Data Using the LinqDataSource Control.

適用於