다음을 통해 공유


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 절을 만드는 데 사용되는 문자열입니다.

예제

다음 예제에서는 라는 Category속성으로 반환된 데이터를 그룹화 하는 컨트롤을 보여 LinqDataSource 옵니다. 공유 값을 반환하고 그룹화된 레코드의 평균 가격을 계산합니다.

<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 줍니다. 속성은 KeyColor두 개의 속성이 있는 개체를 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>

다음 예제에서는 이전 예제에서 LinqDataSource 컨트롤의 데이터를 표시하기 위한 두 개의 ListView 컨트롤을 보여 줍니다. 한 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 쿼리의 모든 레코드가 단일 통합 레코드로 반환됩니다.

함수의 모든 속성을 묶고 각 속성을 GroupBy 쉼표로 구분하여 속성에 new 둘 이상의 속성을 할당할 수 있습니다. 예를 들어 속성을 기준으로 그룹화한 다음 속성을 로 설정합니다 GroupBynew(Name, Category).NameCategory

그룹화에 사용되는 속성의 값은 라는 Key생성된 속성을 통해 반환됩니다. 속성에 Key 속성을 Select 포함하여 그룹화된 값을 검색합니다. 키워드를 Key 사용하여 As 속성을 별칭으로 설정할 수 있지만 별칭을 사용할 필요는 없습니다. 예를 들어 속성을 라는 Category속성으로 설정할 GroupBy 수 있습니다. 속성을 로 설정 Select 하여 속성에서 Category 통합된 값을 검색할 new(Key As ProductCategory)수 있습니다.

속성에 속성을 포함하여 그룹화의 It 개별 레코드에 Select 액세스할 수 있습니다. 속성에는 It 그룹화된 속성의 값을 공유하는 레코드 컬렉션이 포함되어 있습니다. 속성을 반복 It 하여 개별 레코드를 검색할 수 있습니다.

속성은 GroupBy 종종 집계 메서드와 함께 사용됩니다. 다음 집계 메서드를 사용할 수 있습니다.

  • Count()

  • Average()

  • Sum()

  • Max()

  • Min()

  • Where(condition)

  • Any()

  • All(condition)

자세한 내용은 LinqDataSource 웹 서버 컨트롤 개요방법: LinqDataSource 컨트롤을 사용하여 데이터 그룹화 및 집계를 참조하세요.

적용 대상