zoukankan      html  css  js  c++  java
  • IEnumerable的基本使用

    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>();
        }
  • 相关阅读:
    每日作业报告之《大道至简》读后感
    Java学习的第二十六天
    Java学习的第二十五天
    封装按钮组件
    回车导致地址栏出现?
    判断用户输入值的是不是整数
    String类型在JVM中的内存分配
    MySQL索引的原理及使用
    ICPC Yokohama, 2018(Domestic)C. Skyscraper “MinatoHarukas ” (暴力)
    CH6803 导弹发射塔(二分图最大匹配/多重匹配)
  • 原文地址:https://www.cnblogs.com/Zingu/p/14680972.html
Copyright © 2011-2022 走看看