• LINQ 操作符


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Linq;
    namespace LinQ
    {
        class Program
        {
            static void Main(string[] args)
            {
                l1();
                Console.WriteLine();
                l2();
                Console.WriteLine();
                l3();
                Console.WriteLine();
                l4();
                Console.WriteLine();
                l5();
                Console.WriteLine();
                l6();
                Console.WriteLine();
                l7();
                Console.WriteLine();
                l8();
                Console.WriteLine();
                l9();
                Console.WriteLine();
                l10();
                Console.Read();
            }
            public static void l1()
            {
                int[] scores = new int[] { 88, 89, 100, 51, 23, 92, 81, 60 };
                IEnumerable<int> scoreQuery = from scrore in scores where scrore > 70 select scrore;
                foreach (int i in scoreQuery)
                {
                    Console.Write(i + " ");
    
                }
            }
    
            /*linq检索数据转换数据
            *合并两个序列
            */
            class Student
            {
                public string StuNo { get; set; }
                public string StuName { get; set; }
                public string StuCollege { get; set; }
                public List<int> StuScores;
            }
            class Teacher
            {
                public string TeaNo { get; set; }
                public string TeaName { get; set; }
                public string TeaCollege { get; set; }
            }
            public static void l2()
            {
                List<Student> students = new List<Student>() { 
                new Student{StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},
                new Student {StuNo ="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}}
                };
                List<Teacher> teachers = new List<Teacher>() { 
                new Teacher {TeaNo ="001",TeaName ="张华",TeaCollege ="信息工程学院"},
                new Teacher {TeaNo ="002",TeaName ="王丽",TeaCollege ="机电学院"}
                };
                var a = (from student in students where student.StuCollege == "信息工程学院" select student.StuName).Concat(from teacher in teachers
                                                                                                                      where teacher.TeaCollege == "信息工程学院"
                                                                                                                      select teacher.TeaName);
                Console.WriteLine("信息工程学院的老师和同学们有:");
                foreach (var person in a)
                {
                    Console.WriteLine(person);
                }
            }
            /*
            *选择源序列元素的一个或多个属性构成元素
            */
            public static void l3()
            {
                List<Student> students = new List<Student>() { 
                new Student {StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores=new List<int> {97,92,81,60}},
                new Student {StuNo ="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}}
                };
                var a = from student in students select student.StuName;
                foreach (var person in a)
                {
                    Console.WriteLine(person);
                }
                var a1 = from student in students select new { student.StuNo, student.StuName };
                foreach (var person in a1)
                {
                    Console.WriteLine("{0},{1}", person.StuNo, person.StuName);
                }
            }
    
            /*
            * LINQ的推迟查询
            * **/
            public static void l4()
            {
                List<string> Cites = new List<string> { "shanghai", "beijing", "xiamen", "qingdao", "xian" };
                var cityWiths = from c in Cites where c.StartsWith("s") orderby c select c;
                Console.WriteLine("第一次查询名称以‘S’起始的城市");
                foreach (string city in cityWiths)
                {
                    Console.WriteLine(city);
                }
                Console.WriteLine();
                Cites.Add("shengzhen");
                Cites.Add("shenyang");
                Console.WriteLine("第二次查询名称以‘s’起始的城市");
                foreach (string city in cityWiths)
                {
                    Console.WriteLine(city);
                }
            }
    
            /*
            * 标准查询操作符
            * where 过滤操作符定义了返回元素的条件
            * select 投射操作符用于把对象转换成另一个类型的对象
            * orderby 排序操作符改变返回元素的顺序
            * group by 组合运算符数据放在一个数组中
            * Count,Sum,Min,Max,Average 合计操作符计算集合的一个值,利用这些合作操作符,可以计算所有值的总和,元素的个数,值最大和最小的元素,平均值
            * Distinct 从集合中删除重复的元素
            * join 链接运算符用于链接两个集合
            * **/
    
    
    
    
    
            /*
            * where 子句
            * **/
            public static void l5()
            {
                List<Student> students = new List<Student>() { 
                new Student {StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},
                new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}}
                };
                var a = (from student in students
                         where student.StuCollege == "信息工程学院" && student.StuScores.Average() > 75
                         select student.StuName);
                foreach (var person in a)
                {
                    Console.WriteLine(person);
                }
            }
            /*
            * orderby子句
            * **/
            public static void l6()
            {
                List<Student> students = new List<Student>() { 
                new Student {StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},
                new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}}
                };
                var a = from student in students orderby student.StuName descending select student.StuName;
                foreach (var person in a)
                {
                    Console.WriteLine(person);
                }
            }
            /*
            * group by子句
            * **/
            public static void l7()
            {
                List<Student> students = new List<Student>() { 
                new Student {StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},
                new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}},
                new Student {StuNo ="03",StuName ="王五",StuCollege ="机电学院",StuScores =new List<int> {96,95,79,90}}
                };
                var a = from student in students
                        group student by student.StuCollege into collegeGroup
                        select new { College = collegeGroup.Key, Count = collegeGroup.Count() };
                foreach (var person in a)
                {
                    Console.WriteLine("{0}{1}", person.College, person.Count);
                }
            }
            /*
            * 合计操作符
            **/
            public static void l8()
            {
                List<Student> students = new List<Student>() { 
                new Student {StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},
                new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}},
                new Student {StuNo ="03",StuName ="王五",StuCollege ="机电学院",StuScores =new List<int> {96,95,79,90}}
                };
                int a = (from student in students select students).Count();
                //Sum,Min,Max,Average原理与其相同
                Console.WriteLine("学生总数为:");
                Console.WriteLine(a);
    
            }
            /*
            * Distinct运算符
            * 
            **/
            public static void l9()
            {
                List<Student> students = new List<Student>() { 
                new Student {StuNo ="01",StuName ="张三",StuCollege ="信息工程学院",StuScores =new List<int> {97,92,81,60}},
                new Student {StuNo="02",StuName ="李四",StuCollege ="机电学院",StuScores =new List<int> {90,95,78,70}},
                new Student {StuNo ="03",StuName ="王五",StuCollege ="机电学院",StuScores =new List<int> {96,95,79,70}}
                };
                var a = (from student in students select student.StuCollege).Distinct();
                foreach (var college in a)
                {
                    Console.WriteLine(college);
                }
            }
    
            /*
            * join用于链接两个集合查询语法为join...in...on....equals
            * 
            **/
            class Student3
            {
                public string StuNo { get; set; }
                public string StuName { get; set; }
                public string CollegeNo { get; set; }
                public List<int> StuScores;
            }
            class College
            {
                public string CollegeNo { get; set; }
                public string CollegeName { get; set; }
            }
            public static void l10()
            {
                List<Student3> students = new List<Student3>() { 
                new Student3 {StuNo ="01",StuName ="张三",CollegeNo ="001",StuScores =new List<int> {97,92,81,60}},
                new Student3 {StuNo="02",StuName ="李四",CollegeNo ="002",StuScores =new List<int> {90,95,78,70}},
                new Student3 {StuNo ="03",StuName ="王五",CollegeNo ="003",StuScores =new List<int> {96,95,79,70}}
                };
                            List<College> colleges = new List<College>() { 
                new College {CollegeNo="001",CollegeName ="信息工程学院" },
                new College {CollegeNo ="002",CollegeName ="机电学院"}
                };
                var a = from student in students
                        join college in colleges on student.CollegeNo equals college.CollegeNo
                        select new { student.StuName, college.CollegeName };
                foreach (var person in a)
                {
                    Console.WriteLine("{0 } {1}", person.StuName, person.CollegeName);
                }
    
            }
        }
    }
  • 相关阅读:
    [HDU6146]Pokémon GO
    [BZOJ1406]密码箱
    [BZOJ1045]糖果传递
    [Codeforces613E]Puzzle Lover
    [Codeforces778E]Selling Numbers
    [Codeforces477D]Dreamoon and Binary
    [Codeforces440D]Berland Federalization
    [Codeforces441E]Valera and Number
    文件上传漏洞(绕过姿势)
    WebShell代码分析溯源(第1题)墨者学院
  • 原文地址:https://www.cnblogs.com/sumg/p/3743066.html
Copyright © 2020-2023  润新知