static void Main(string[] args) { var stu1 = new Student() { Name = "张三", Age = 18, Sallry = 9000 }; var stu2 = new Student() { Name = "李四", Age = 15, Sallry = 2000 }; var stu3 = new Student() { Name = "王五", Age = 13, Sallry = 7000 }; var stu4 = new Student() { Name = "赵六", Age = 17, Sallry = 4000 }; var stu5 = new Student() { Name = "孙琦", Age = 18, Sallry = 5000 }; List<Student> list = new List<Student> { stu1, stu2, stu3, stu4, stu5 }; var th1 = new Teacher() { TeacherName = "老大" }; th1.students.Add(stu1); th1.students.Add(stu3); th1.students.Add(stu5); var th2 = new Teacher() { TeacherName = "王二" }; th2.students.Add(stu1); th2.students.Add(stu2); th2.students.Add(stu4); Teacher[] ts = { th1, th2 }; Console.WriteLine(ts.Any()); Console.WriteLine(ts.Any(p=>p.TeacherName=="是")); //var li = list.OrderByDescending(p => p.Age); //倒序 OrderByDescending var li = list.OrderBy(p => p.Age); //正序 OrderBy foreach (var item in li) { Console.WriteLine(item); } Console.WriteLine("----------------------排序分割线-------------------"); var li1 = list.OrderByDescending(p => p.Age).ThenByDescending(p=>p.Sallry);//多条件 ThenByDescending/ ThenBy foreach (var item in li1) { Console.WriteLine(item); } var li3 = list.Skip(2); //跳过前一条 Skip(2) foreach (var item in li3) { Console.WriteLine(item); } Console.WriteLine("----------------------分页分割线-------------------"); var li4 = list.Skip(2).Take(2); ///跳过多少条取多少条 Skip(?).Take(?) foreach (var item in li4) { Console.WriteLine(item); } Console.WriteLine("----------------------差集/并集分割线-------------------"); //差集 int[] num1 = { 1, 2, 4, 6 }; int[] num2 = { 4, 6, 7, 8 }; var num= num1.Except(num2); //差集 输出num1在num2不存在的数据 foreach (var item in num) { Console.WriteLine(item); } var numAll = num1.Union(num2); //并集 输出num1和num2去重后的元素 foreach (var item in numAll) { Console.WriteLine(item); } Console.WriteLine("----------------------分组分割线-------------------"); IEnumerable<IGrouping<int,Student>> ist= list.GroupBy(p => p.Age); foreach (var item in ist) { Console.WriteLine(item.Key); foreach (var deatail in item) { Console.WriteLine(deatail); } } Console.WriteLine("----------------------属性集合分割线-------------------"); var sts = ts.SelectMany(s=>s.students); foreach (var item in sts) { Console.WriteLine(item); } }
class Student { public string Name { get; set; } public int Age { get; set; } public decimal Sallry { get; set; } public override string ToString() { return $"名称:{Name}-年龄:{Age}-收入:{Sallry}"; } } class Teacher { public string TeacherName { get; set; } public List<Student> students { get; set; } = new List<Student>(); }