在 LINQ 中,GroupBy 运算符用于根据键的指定值对列表/集合项进行分组,并返回 IGrouping<key, Values> 的集合。 LINQ 中的 GroupBy 方法与 SQL group by 语句相同。
LINQ GroupBy() 方法的语法
下面是使用 LINQ GroupBy() 方法根据键的指定值对元素进行分组的语法。
var student1 = objStudent.GroupBy(x => x.Location);
根据上述语法,我们根据学生位置对集合的 "objStudent" 项进行分组。
方法语法中的 LINQ GroupBy() 示例
这是在方法语法中使用 LINQ GroupBy() 的示例。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Programme2
{
static void Main(string[] args)
{
//Create an object 'objStudent' of the list of the student
List<Student> objStudent = new List<Student>
()
{
new Student() { Name = "Ak Tyagi", Gender = "Male",Location="Chennai" },
new Student() { Name = "Rohini", Gender = "Female", Location="Chennai" },
new Student() { Name = "Praveen", Gender = "Male",Location="Bangalore" },
new Student() { Name = "Sateesh", Gender = "Male", Location ="Vizag"},
new Student() { Name = "Madhav", Gender = "Male", Location="Nagpur"}
};
// here with the help of GrouBy we will fetch the student on the base of location
var student1 = objStudent.GroupBy(x => x.Location);
foreach (var sitem in student1)
{
// WriteLine() function here count the number of student
Console.WriteLine(sitem.Key, sitem.Count());
Console.WriteLine();
foreach (var stud in sitem)
{
//Console.WriteLine(stud.Name + "\t" + stud.Location) show the information of the student on the base of the location
Console.WriteLine(stud.Name + "\t" + stud.Location);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
//Student class containing the name of the student,gender and location of the student
class Student
{
public string Name { get; set; }
public string Gender { get; set; }
public string Location { get; set; }
}
}
在上面的例子中,我们根据学生的位置对 "objStudent" 中的项目集合进行分组。
输出:
查询语法中的 LINQ GroupBy() 示例
这是使用 LINQ GroupBy() 运算符的示例。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Programme2
{
static void Main(string[] args)
{
//create an object objStudent of the Student List and add some information
List<Student> objStudent = new List<Student>
()
{
new Student() { Name = "Vinay Tyagi", Gender = "Male",Location="Chennai" },
new Student() { Name = "Rohini", Gender = "Female", Location="Chennai" },
new Student() { Name = "Praveen", Gender = "Male",Location="Bangalore" },
new Student() { Name = "Sateesh Alavala", Gender = "Male", Location ="Vizag"},
new Student() { Name = "Madhav Sai", Gender = "Male", Location="Nagpur"}
};
//apply the linq queries to group the information of the student according to the location
/*linq queries starts from, from take a variable 'std' in
objStudent(object of the Student List) group 'std'(declared variable) by std.Location*/
var student = from std in objStudent
group std by std.Location;
//foreach loop iterate over all the information of the student
foreach (var sitem in student)
{
Console.WriteLine(sitem.Key, sitem.Count());
Console.WriteLine();
foreach (var stud in sitem)
{
/*Console.WriteLine(stud.Name + "\t" + stud.Location) show the
information of the student on the base of the location*/
Console.WriteLine(stud.Name + "\t" + stud.Location);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
class Student
{
public string Name { get; set; }
public string Gender { get; set; }
public string Location { get; set; }
}
}
输出:
相关用法
- LINQ AsEnumrable()用法及代码示例
- LINQ ToLookup()用法及代码示例
- LINQ ElementAtOrDefault()用法及代码示例
- LINQ Count()用法及代码示例
- LINQ Single()用法及代码示例
- LINQ sum()用法及代码示例
- LINQ ToArray()用法及代码示例
- LINQ Min()用法及代码示例
- LINQ ToDictionary()用法及代码示例
- LINQ LastOrDefault()用法及代码示例
- LINQ FirstOrDefault()用法及代码示例
- LINQ Cast()用法及代码示例
- LINQ ToList()用法及代码示例
- LINQ Max()用法及代码示例
- LINQ Aggregate()用法及代码示例
- LINQ DefaultfEmpty()用法及代码示例
- LINQ Last()用法及代码示例
- LINQ ElementAt()用法及代码示例
- LINQ OfType()用法及代码示例
- Lodash _.sampleSize()用法及代码示例
注:本文由纯净天空筛选整理自 LINQ GroupBy() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。