在 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。