C# 当中 LINQ 的常规用法(Lambda 方式) (3)

标准用法:

public class Student { public string Name { get; set; } public bool Graduation { get; set; } public int Age { get; set; } public int Score { get; set; } public string City { get; set; } public override string ToString() { return $"姓名:{Name},年龄:{Age},分数:{Score},是否毕业:{Graduation},城市:{City}"; } } class Program { static void Main(string[] args) { var students = new List<Student> { new Student{Name = "张三",Age = 15,Score = 94,Graduation = true,City = "北京"}, new Student{Name = "李四",Age = 17,Score = 47,Graduation = false,City = "北京"}, new Student{Name = "王二",Age = 19,Score = 77,Graduation = false,City = "广州"}, new Student{Name = "孙五",Age = 14,Score = 14,Graduation = false,City = "上海"} }; var groupByResult = students.GroupBy(x => x.City,student=>new{student.Name,student.City}); foreach (var item in groupByResult) { Console.WriteLine($"分组城市:{item.Key}"); foreach (var student in item) { Console.WriteLine($"姓名:{student.Name},城市:{student.City}"); } } } }

输出结果:

标准用法:

分组城市:北京 姓名:张三,城市:北京 姓名:李四,城市:北京 分组城市:广州 姓名:王二,城市:广州 分组城市:上海 姓名:孙五,城市:上海

方法 3

public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector);

本方法重载与之前的方法不一样,没有了 elementSelector 参数,变成了 resultSelector 参数,并且其返回值也由 IEnumerable<IGrouping<TKey, TSource>> 变成了 IEnumerable<YResult> 。

在这个方法当中,我们只需要通过 resultSelector 委托即可指定需要输出的数据类型,这里该委托的 TKey 参数是分组的列/属性,而 IEnumerable<TSource> 则是每个分组结果的关联序列。

标准用法:

public class Student { public string Name { get; set; } public bool Graduation { get; set; } public int Age { get; set; } public int Score { get; set; } public string City { get; set; } public override string ToString() { return $"姓名:{Name},年龄:{Age},分数:{Score},是否毕业:{Graduation},城市:{City}"; } } class Program { static void Main(string[] args) { var students = new List<Student> { new Student{Name = "张三",Age = 15,Score = 94,Graduation = true,City = "北京"}, new Student{Name = "李四",Age = 17,Score = 47,Graduation = false,City = "北京"}, new Student{Name = "王二",Age = 19,Score = 77,Graduation = false,City = "广州"}, new Student{Name = "孙五",Age = 14,Score = 14,Graduation = false,City = "上海"} }; var groupByResult = students.GroupBy(x => x.City, (key, enumerable) => { return new { City = key, Avg = enumerable.Average(x => x.Score), Max = enumerable.Max(x => x.Score) }; }); foreach (var student in groupByResult) { Console.WriteLine($"城市:{student.City},平均分:{student.Avg},最高分:{student.Max}"); } } }

输出结果:

城市:北京,平均分:70.5,最高分:94 城市:广州,平均分:77,最高分:77 城市:上海,平均分:14,最高分:14

方法 4

public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector);

最后一个方法与之前的方法一样,不过多了 elementSelector,与第二个方法一样,用于指定要选择的属性字段,排除其他字段。

标准用法:

public class Student { public string Name { get; set; } public bool Graduation { get; set; } public int Age { get; set; } public int Score { get; set; } public string City { get; set; } public override string ToString() { return $"姓名:{Name},年龄:{Age},分数:{Score},是否毕业:{Graduation},城市:{City}"; } } class Program { static void Main(string[] args) { var students = new List<Student> { new Student{Name = "张三",Age = 15,Score = 94,Graduation = true,City = "北京"}, new Student{Name = "李四",Age = 17,Score = 47,Graduation = false,City = "北京"}, new Student{Name = "王二",Age = 19,Score = 77,Graduation = false,City = "广州"}, new Student{Name = "孙五",Age = 14,Score = 14,Graduation = false,City = "上海"} }; var groupByResult = students.GroupBy(x => x.City, x=>new{x.Name,x.City,x.Age,x.Score},(key, enumerable) => { return new { City = key, Avg = enumerable.Average(x => x.Score), Max = enumerable.Max(x => x.Score), AvgAge = enumerable.Average(x=>x.Age) }; }); foreach (var student in groupByResult) { Console.WriteLine($"城市:{student.City},平均分:{student.Avg},最高分:{student.Max},平均年龄:{student.AvgAge}"); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzjdw.html