linq中的分组和排序
一、分组 group 组内成员 by 分组条件 into 组的信息
class Program { static void Main(string[] args) { string[] name = { "张三","张六","刘大","刘晓","刘大脑袋","王大锤"}; var result = from n in name group n by n[0] into g select g; foreach (var g in result) { Console.WriteLine(g.Key+""+g.Count()+"人"); foreach (var item in g) { Console.WriteLine(item); } } } }
二、order by 排序
class Program { static void Main(string[] args) { //降序排序,默认是升序 List<int> nums = new List<int> { 12,46,55,87,45,12,12,1,2,3}; //方法1 linq语句+linq方法 var result = from i in nums orderby i descending select i; foreach (var i in result) { Console.WriteLine(i); } //linq方法 var num= nums.OrderByDescending(i => i); foreach (var i in num) { Console.WriteLine(i); } } }