A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/queries/group-by-clause below:

Group By Clause - Visual Basic

Groups the elements of a query result. Can also be used to apply aggregate functions to each group. The grouping operation is based on one or more keys.

Syntax
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ]  
  Into aggregateList  
Parts

You can use the Group By clause to break the results of a query into groups. 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.

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. You can also include aggregate functions in the Into clause to compute values for the grouped elements. 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
See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4