共用方式為


HOW TO:使用 LinqDataSource 控制項分組和彙總資料

更新:2007 年 11 月

LinqDataSource 控制項可讓您依照一個或多個資料行將資料來源的資料分組。當您要擷取資料來源中所有資料錄 (共用已分組之資料行的值) 的相關資訊時,可以依照資料行將資料錄分組。通常,您會使用已分組之資料上的彙總函式 (Aggregate Function) 來計算例如加總、平均或計數等值。

若要定義用於將資料分組的資料行,您必須設定 LinqDataSource 控制項的 GroupBy 屬性。當您想要使用彙總函式時,您也必須指定 Select 屬性的值,以定義彙總函式。

注意事項:

當您要使用的資料處於已分組狀態時,無法更新、插入或刪除資料錄。

依照一個資料行來分組與彙總資料

您可以依照單一資料行將資料分組,方式是在 GroupBy 屬性中指定資料行。

依照一個資料行來分組與彙總

  1. LinqDataSource 控制項的 ContextTypeName 設定至資料來源。

  2. TableName 屬性設定為包含要分組之資料的資料來源物件中的屬性。

  3. GroupBy 屬性設定為要用於將資料分組的資料行。

  4. 設定 Select 屬性以包含彙總函式與用於將資料分組的資料行。

    GroupBy 屬性中指定的屬性透過名為 Key 的物件所擷取。您必須使用 As 關鍵字指派名稱 (別名) 至彙總函式,以便資料繫結控制項可以參考由彙總函式所建立的屬性。若彙總函式沒有名稱,則 LinqDataSourceView 物件會引發例外狀況。

    下列範例顯示 LinqDataSource 控制項,此控制項使用名為 ProductCategory 的資料行將資料分組,並使用 GridView 控制項來顯示結果。它會透過 Key 屬性選取 ProductCategory 值。接著,它會計算具有相同 ProductCategory 值之產品的 ListPrice 與 Cost 屬性的平均值。它也會傳回每個 ProductCategory 值之資料錄的數目計數。

    <asp:LinqDataSource 
      ContextTypeName="ExampleDataContext" 
      TableName="Products" 
      GroupBy="ProductCategory"
      Select="new(Key, 
        Average(ListPrice) As AverageListPrice, 
        Average(Cost) As AverageCost, 
        Count() As RecordCount)"
      ID="LinqDataSource1" 
      >
    </asp:LinqDataSource>
    <asp:GridView 
      DataSourceID="LinqDataSource1" 
      ID="GridView1" 
      >
    </asp:GridView>
    

依照多個資料行來分組與彙總

若要依照多個資料行將資料分組,當您設定 GroupBy 屬性時必須使用 new 函式。

依照多個資料行來分組

  1. 設定 ContextTypeName 屬性與 TableName 屬性,如上述程序中所述。

  2. 使用如 new(column1, column2) 的語法來設定 GroupBy 屬性,其中 column1 與 column2 是要用於將資料分組之資料行的名稱。您可以視需要提供多個資料行。

  3. 設定 Select 屬性以包含 Key 屬性與所需的任何彙總函式。

    下列範例顯示 LinqDataSource 控制項,此控制項設定為依照兩個資料行來進行分組,並在 DataList 控制項中顯示結果。Key 物件包含兩個屬性,也就是 ProductCategory 與 Color。

    <asp:LinqDataSource 
      ContextTypeName="ExampleDataContext" 
      TableName="Products" 
      GroupBy="new(ProductCategory,Color)"
      Select="new(Key,
        Average(ListPrice) as AverageListPrice, 
        Count() as RecordCount)"
      ID="LinqDataSource1" 
      >
    </asp:LinqDataSource>
    <asp:DataList 
      DataSourceID="LinqDataSource1" 
      ID="DataList1" 
      >
      <ItemTemplate>
        <%# Eval("Key.ProductCategory") %> 
        <%# Eval("Key.Color") %> 
        <%# Eval("AverageListPrice") %> 
        <%# Eval("RecordCount") %> 
      </ItemTemplate>
    </asp:DataList>
    

在將資料分組時擷取個別資料錄

當您依照一個或多個資料行將資料分組時,可以使用 It 關鍵字來擷取每個群組中的個別資料錄。此關鍵字表示資料物件的目前執行個體。傳回的資料將會同時包含已分組的資料與屬於該群組的資料錄。

擷取並顯示已分組的資料與個別資料錄

  1. 設定 ContextTypeName 屬性,以及 LinqDataSource 控制項的 TableName 屬性。

  2. GroupBy 屬性設定為要用於將資料分組的一個或多個資料行。

  3. 設定 Select 屬性以包含 It 關鍵字與所需的任何彙總函式。您必須使用 As 關鍵字來重新命名 It 關鍵字所代表的物件。

    下列範例顯示 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" 
      >
    </asp:LinqDataSource>
    

    下列範例顯示兩個 ListView 控制項,它們會顯示屬於該群組之產品的已分組資料與個別名稱。巢狀資料繫結控制項的 DataSource 屬性設定為 Products,它是 it 物件的別名。

    <asp:ListView 
      DataSourceID="LinqDataSource1" 
      ID="ListView1" 
      >
    
      <LayoutTemplate>
        <table style="background-color:Teal;color:White" 
    
          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>
          <tbody  id="itemContainer">
          </tbody>
        </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">
            <asp:ListView 
              DataSource='<%# Eval("Products") %>' 
    
              ID="ListView2">
    
              <LayoutTemplate>
                <div 
                  style=" width:100%;background-color:White;color:Black" 
    
                  id="itemContainer">
                </div>
              </LayoutTemplate>
    
              <ItemTemplate>
                <%# Eval("ProductName") %><br />
              </ItemTemplate>
    
            </asp:ListView> 
          </td>
        </tr>
      </ItemTemplate>
    </asp:ListView>
    

請參閱

概念

LinqDataSource Web 伺服器控制項概觀