Group By 句 (Visual Basic)Group By Clause (Visual Basic)
クエリ結果の要素をグループ化します。Groups the elements of a query result. これを使用して、グループごとに集計関数を適用することもできます。Can also be used to apply aggregate functions to each group. グループ化操作は、1 つ以上のキーに基づきます。The grouping operation is based on one or more keys.
構文Syntax
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ]
Into aggregateList
指定項目Parts
listField1
,listField2
listField1
,listField2
任意。Optional. グループ化された結果に含めるフィールドを明示的に示す、クエリ変数の 1 つ以上のフィールドです。One or more fields of the query variable or variables that explicitly identify the fields to be included in the grouped result. フィールドを指定しない場合、グループ化された結果にはクエリ変数のすべてのフィールドが含まれます。If no fields are specified, all fields of the query variable or variables are included in the grouped result.
keyExp1
必須です。Required. 要素のグループを決定するために使用するキーを識別する式です。An expression that identifies the key to use to determine the groups of elements. 複数のキーを指定して、複合キーを指定できます。You can specify more than one key to specify a composite key.
keyExp2
任意。Optional.
keyExp1
と結合して複合キーを作成する 1 つ以上の追加キーです。One or more additional keys that are combined withkeyExp1
to create a composite key.aggregateList
必須です。Required. グループの集計方法を示す 1 つ以上の式です。One or more expressions that identify how the groups are aggregated. グループ化された結果のメンバー名を示すには、次のいずれかの形式で、
Group
キーワードを使用します。To identify a member name for the grouped results, use theGroup
keyword, which can be in either of the following forms:Into Group
- または --or-
Into <alias> = Group
グループに適用する集計関数を含めることもできます。You can also include aggregate functions to apply to the group.
RemarksRemarks
Group By
句を使用して、クエリの結果をグループに分割できます。You can use the Group By
clause to break the results of a query into groups. グループ化は、1 つのキー、または複数のキーで構成される複合キーに基づいて行われます。The grouping is based on a key or a composite key consisting of multiple keys. 一致するキー値と関連付けられた要素は、同じグループに入れられます。Elements that are associated with matching key values are included in the same group.
グループの参照に使用するメンバー名を示すには、 aggregateList
句の Into
パラメーターと Group
キーワードを使用します。You use the aggregateList
parameter of the Into
clause and the Group
keyword to identify the member name that is used to reference the group. Into
句に集計関数を含めることで、グループ化された要素の値を計算することもできます。You can also include aggregate functions in the Into
clause to compute values for the grouped elements. 標準的な集計関数の一覧については、「 Aggregate Clause」をご覧ください。For a list of standard aggregate functions, see Aggregate Clause.
例Example
以下のコード例では、場所 (国/地域) に基づいて顧客の一覧をグループ化し、各グループ内の顧客の数を返します。The following code example groups a list of customers based on their location (country/region) and provides a count of the customers in each group. 結果は、国/地域名によって並べ替えられます。The results are ordered by country/region name. グループ化した結果は、市区町村名によって並べ替えられます。The grouped results are ordered by city name.
Public Sub GroupBySample()
Dim customers = GetCustomerList()
Dim customersByCountry = From cust In customers
Order By cust.City
Group By CountryName = cust.Country
Into RegionalCustomers = Group, Count()
Order By CountryName
For Each country In customersByCountry
Console.WriteLine(country.CountryName &
" (" & country.Count & ")" & vbCrLf)
For Each customer In country.RegionalCustomers
Console.WriteLine(vbTab & customer.CompanyName &
" (" & customer.City & ")")
Next
Next
End Sub