Group By (Cláusula, Visual Basic)Group By Clause (Visual Basic)
Agrupa los elementos de los resultados de una consulta.Groups the elements of a query result. También se puede usar para aplicar funciones de agregado a cada grupo.Can also be used to apply aggregate functions to each group. La operación de agrupación se basa en una o varias claves.The grouping operation is based on one or more keys.
SintaxisSyntax
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ]
Into aggregateList
ElementosParts
listField1
,listField2
listField1
,listField2
Opcional.Optional. Uno o más campos de la variable o las variables de consulta que identifican explícitamente los campos que se incluirán en el resultado agrupado.One or more fields of the query variable or variables that explicitly identify the fields to be included in the grouped result. Si no se especifica ningún campo, se incluyen todos los campos de la variable o las variables de consulta en el resultado agrupado.If no fields are specified, all fields of the query variable or variables are included in the grouped result.
keyExp1
Obligatorio.Required. Expresión que identifica la clave que se va a usar para determinar los grupos de elementos.An expression that identifies the key to use to determine the groups of elements. Puede especificar más de una clave para especificar una clave compuesta.You can specify more than one key to specify a composite key.
keyExp2
Opcional.Optional. Uno o más claves adicionales que se combinan con
keyExp1
para crear una clave compuesta.One or more additional keys that are combined withkeyExp1
to create a composite key.aggregateList
Obligatorio.Required. Una o más expresiones que identifican cómo se agregan los grupos.One or more expressions that identify how the groups are aggregated. Para identificar un nombre de miembro para los resultados agrupados, use la palabra clave
Group
, que puede estar en cualquiera de los formatos siguientes:To identify a member name for the grouped results, use theGroup
keyword, which can be in either of the following forms:Into Group
O bien,-or-
Into <alias> = Group
También puede incluir funciones de agregado para aplicar al grupo.You can also include aggregate functions to apply to the group.
ComentariosRemarks
Puede usar la cláusula Group By
para dividir los resultados de una consulta en grupos.You can use the Group By
clause to break the results of a query into groups. La agrupación se basa en una clave o una clave compuesta formada por varias claves.The grouping is based on a key or a composite key consisting of multiple keys. Los elementos que están asociados con valores de clave coincidentes se incluyen en el mismo grupo.Elements that are associated with matching key values are included in the same group.
El parámetro aggregateList
de la cláusula Into
y la palabra clave Group
se usan para identificar el nombre del miembro usado para hacer referencia al grupo.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. También puede incluir funciones de agregado en la cláusula Into
para calcular los valores de los elementos agrupados.You can also include aggregate functions in the Into
clause to compute values for the grouped elements. Para obtener una lista de las funciones de agregado estándar, consulte Aggregate Clause.For a list of standard aggregate functions, see Aggregate Clause.
EjemploExample
En el ejemplo de código siguiente se agrupa una lista de clientes según su ubicación (país o región) y se proporciona un recuento de los clientes de cada grupo.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. Los resultados se ordenan por nombre de país o región.The results are ordered by country/region name. Los resultados agrupados se ordenan por nombre de ciudad.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
Vea tambiénSee also
Comentarios
Cargando comentarios...