在LINQ中,分组运算符选择包含公共属性的序列或集合的元素,然后将它们作为一个组来提供服务。换句话说,我们可以说分组运算符基于给定的键返回元素组。该组保存在一种特殊的集合中,该集合实现IGrouping
标准查询运算符包含2种不同类型的分组运算符:
- 通过…分组
- 去查查看
GroupBy运算符
GroupBy运算符的工作方式类似于SQL GroupBy子句。它用于返回共享给定序列或集合中的公共属性或键的元素组。每个组均由IGrouping
重要事项:
- 它支持C#和VB.Net语言的查询语法。如示例1所示。
- 您也可以在C#查询中与GroupBy一起使用。使用in关键字可以继续查询,并可以执行更多查询操作。换句话说,它是一个临时标识符,可让您执行其他查询操作。
- 您还可以在VB.Net中将Into Group与GroupBy一起使用。
- LINQ查询以帮助Select或Groupby子句结束。
- 它还可以支持C#和VB.Net语言的方法语法。如示例2所示。
- 它同时存在于Queryable和Enumerable类中。在这些类中,GroupBy方法在8种不同类型中被重载。
- 它是通过使用延迟执行来实现的。
- 键值可以是字符串,整数,浮点数,匿名,布尔等任意类型,具体取决于用户的要求。
- 该运算符返回的组可以包含零个或多个与键值匹配的元素。
范例1:
// C# program to divide the employees
// in groups according to their salary
using System;
using System.Linq;
using System.Collections.Generic;
// Employee details
public class Employee {
public int emp_id
{
get;
set;
}
public string emp_name
{
get;
set;
}
public string emp_gender
{
get;
set;
}
public string emp_hire_date
{
get;
set;
}
public int emp_salary
{
get;
set;
}
}
class GFG {
// Main method
static public void Main()
{
List emp = new List() {
new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
emp_hire_date = "12/3/2017", emp_salary = 20000},
new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
emp_hire_date = "22/4/2018", emp_salary = 30000},
new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
emp_hire_date = "3/5/2016", emp_salary = 40000},
new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
emp_hire_date = "4/8/2017", emp_salary = 40000},
new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
emp_hire_date = "12/1/2016", emp_salary = 40000},
new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
emp_hire_date = "17/6/2015", emp_salary = 50000},
};
// Query to divide the employees
// in the groups according to
// their salary using GroupBy
// operator in query syntax
var res = from e in emp
group e by e.emp_salary;
foreach(var val in res)
{
// Here salary is the key value
Console.WriteLine("Group By Salary: {0}", val.Key);
// Display name of the employees
// Inner collection according to
// the key value
foreach(Employee e in val)
{
Console.WriteLine("Employee Name: {0}",
e.emp_name);
}
}
}
}
输出:
Group By Salary: 20000
Employee Name: Anjita
Group By Salary: 30000
Employee Name: Soniya
Group By Salary: 40000
Employee Name: Rohit
Employee Name: Supriya
Employee Name: Anil
Group By Salary: 50000
Employee Name: Anju
范例2:
// C# program to divide the employees
// in the groups according to their
// language
using System;
using System.Linq;
using System.Collections.Generic;
// Employee details
public class Employee {
public int emp_id
{
get;
set;
}
public string emp_name
{
get;
set;
}
public string emp_gender
{
get;
set;
}
public string emp_hire_date
{
get;
set;
}
public int emp_salary
{
get;
set;
}
public string emp_lang
{
get;
set;
}
}
class GFG {
// Main method
static public void Main()
{
List emp = new List() {
new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
emp_hire_date = "12/3/2017", emp_salary = 20000, emp_lang = "Ruby"},
new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
emp_hire_date = "22/4/2018", emp_salary = 30000, emp_lang = "Java"},
new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
emp_hire_date = "3/5/2016", emp_salary = 40000, emp_lang = "Perl"},
new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
emp_hire_date = "4/8/2017", emp_salary = 40000, emp_lang = "Java"},
new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
emp_hire_date = "12/1/2016", emp_salary = 40000, emp_lang = "C#"},
new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
emp_hire_date = "17/6/2015", emp_salary = 50000, emp_lang = "C#"},
};
// Query to divide the employees in
// the groups according to their
// language using GroupBy method in
// the method syntax
var res = emp.GroupBy(e => e.emp_lang);
foreach(var val in res)
{
// Here language is the key value
Console.WriteLine("Group By Language: {0}", val.Key);
// Display name of the employees
// Inner collection according to
// the key value
foreach(Employee e in val)
{
Console.WriteLine("Employee Name: {0}", e.emp_name);
}
}
}
}
输出:
Group By Language: Ruby
Employee Name: Anjita
Group By Language: Java
Employee Name: Soniya
Employee Name: Supriya
Group By Language: Perl
Employee Name: Rohit
Group By Language: C#
Employee Name: Anil
Employee Name: Anju