一、Group用法
在C#的LINQ中,Grou将集合中的元素按照指定的键进行分组。Group方法返回一个IEnumerable<IGrouping<TKey, TElement>>类型的集合,其中TKey表示分组的键类型,TElement表示集合中元素的类型。每个IGrouping<TKey, TElement>对象表示一个分组,其中Key属性表示分组的键,而IEnumerable<TElement>表示分组中的元素集合。
1、groupby的定义:
//Linq语句
var tt = from e in stuList group e by e.Math;
//lambda 表达式
var tt = stuList.GroupBy(p=>p.Math);
示例代码:
List<Student_1> stuList = new List<Student_1>()
{new Student_1(){ID=1,Name="John",Chinese=92,Math=88,English=92},new Student_1(){ID=2,Name="Mary",Chinese=87,Math=94,English=82},new Student_1(){ID=3,Name="KangKang",Chinese=89,Math=91,English=96},new Student_1(){ID=4,Name="Maria",Chinese=88,Math=65,English=94},new Student_1(){ID=5,Name="Ben",Chinese=70,Math=91,English=82},
};
//var tt = from e in stuList group e by e.Math;//linq语句var tt = stuList.GroupBy(p=>p.Math);//lambda表达式
foreach (var item in tt)
{Console.WriteLine(item.Key);foreach (var e in item){Console.WriteLine(" "+e.Name);}
}
运行结果为:
2、group...into...用法可以生成一个temp组
var tt = from e in stuListgroup new { e.Math, e.ID, e.Name } by e.Math into temp select new { temp.Key, value1 = temp.Select(p => new { p.ID, p.Name }), value2 = temp.Select(p => p.Name) };
// var tt = stuList.GroupBy(p => p.Math, e => new { e.Math, e.ID, e.Name }).Select(temp => new { temp.Key, value1 = temp.Select(p => new { p.ID, p.Name }), value2 = temp.Select(p => p.Name) });
foreach (var item in tt)
{Console.WriteLine(item.Key);foreach (var e in item.value1){Console.WriteLine(e.Name);}
}
运行结果为:
二、Join用法
Linq中的join用于将两个集合中的元素进行匹配,返回一个新的集合。
1、内部链接 Join (join… in… on …equals…)
定义:
//使用查询语句 Linq
var list = from T1 in dataSource1join T2 in dataSource2 on T1.ID equals T2.IDselect new {Name = T1.Name,ID=T1.ID,NickName = T2.NickName};//使用查询方法 lambda 表达式
var list = dataSource1.Join(dataSource2,T1 => T1.ID,T2 => T2.ID, (T1,T2) => new {Name = T1.Name,ID=T1.ID,NickName = T2.NickName
});
代码示例:
List<Student> students = new List<Student>()
{new Student() { Id = 1, Name = "Tom" },new Student() { Id = 2, Name = "Jerry" },new Student() { Id = 3, Name = "Mike" }
};List<Course> courses = new List<Course>()
{new Course() { Id = 1, Name = "Math", StudentId = 1 },new Course() { Id = 2, Name = "English", StudentId = 1 },new Course() { Id = 3, Name = "Math", StudentId = 2 },new Course() { Id = 4, Name = "Science", StudentId = 3 }
};// 使用join将学生和课程进行匹配
var result = from s in studentsjoin c in courses on s.Id equals c.StudentIdselect new { StudentName = s.Name, CourseName = c.Name };
// 输出结果
foreach (var item in result)
{Console.WriteLine("{0}选了{1}课程", item.StudentName, item.CourseName);
}
运行结果为:
2、分组链接Groupjoin (join…in…on…equals…into…)
定义:
//lambda 表达式
var result = students.GroupJoin(courses, p => p.Id,s => s.StudentId,(p, e) => new { StudentName = p.Name, CourseName =e.Select(m=>m.Name)});//linq 语句
var result = from left in students join right in courses on left.Id equals right.StudentId into temp select new { StudentName = left.Name, CourseName = temp.Select(m => m.Name) };
// temp 其实为course的分组
代码示例:
// 创建学生和课程集合
List<Student> students = new List<Student>()
{new Student() { Id = 1, Name = "Tom" },new Student() { Id = 2, Name = "Jerry" },new Student() { Id = 3, Name = "Mike" }
};List<Course> courses = new List<Course>()
{new Course() { Id = 1, Name = "Math", StudentId = 1 },new Course() { Id = 2, Name = "English", StudentId = 1 },new Course() { Id = 3, Name = "Math", StudentId = 2 },new Course() { Id = 4, Name = "Science", StudentId = 3 }
};
// var result = students.GroupJoin(courses, p => p.Id,s => s.StudentId,(p, e) => new { StudentName = p.Name, CourseName =e.Select(m=>m.Name)});
var result = from left in students join right in courses on left.Id equals right.StudentId into temp select new { StudentName = left.Name, CourseName = temp.Select(m => m.Name) };
// temp 其实为course的分组
// 使用join将学生和课程进行匹配
//var result = from s in students
// join c in courses on s.Id equals c.StudentId
// select new { StudentName = s.Name, CourseName = c.Name };
// 输出结果
foreach (var item in result)
{Console.WriteLine("{0}选了课程", item.StudentName);foreach (var n in item.CourseName){Console.WriteLine("课程为:{0}",n);}
}
运行结果为:
通过对比可以发现,join和groupjion的区别在于,join相当于左右一对一关系,例如:
左1=右1,左1=右2....这样一一列举,但groupjoin不一样,他是以左一为关键字,将左一的值生成一个list,例如 左1={右1,右2}...,在实际应用中根据需求选用不同的用法。