• LINQ 学习路程 -- 查询操作 Average Count Max Sum


    IList<int> intList = new List<int>>() { 10, 20, 30 };
    
    var avg = intList.Average();
    
    Console.WriteLine("Average: {0}", avg);
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } 
        };
    
    var avgAge = studentList.Average(s => s.Age);
    
    Console.WriteLine("Average Age of Student: {0}", avgAge);
    var totalAge = (from s in studentList
                    select s.age).Count(); 
    int Count<TSource>();
    
    int Count<TSource>(Func<TSource, bool> predicate);
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Mathew" , Age = 15 } 
        };
    
    var numOfStudents = studentList.Count();
    
    Console.WriteLine("Number of Students: {0}", numOfStudents);
    // Student collection
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Mathew" , Age = 15 } 
        };
    
    var numOfStudents = studentList.Count(s => s.Age >= 18);
    
    Console.WriteLine("Number of Students: {0}", numOfStudents);
    IList<int> intList = new List<int>() { 10, 21, 30, 45, 50, 87 };
    
    var largest = intList.Max();
    
    Console.WriteLine("Largest Element: {0}", largest);
    
    var largestEvenElements = intList.Max(i => {
                                        if(i%2 == 0)
                                            return i;
                
                                        return 0;
                                    });
    
    Console.WriteLine("Largest Even Element: {0}", largestEvenElements );
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } 
        };
    
    var oldest = studentList.Max(s => s.Age);
    
    Console.WriteLine("Oldest Student Age: {0}", oldest);
    public class Student : IComparable<Student> 
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public int Age { get; set; }
        public int StandardID { get; set; }
    
        public int CompareTo(Student other)
        {
            if (this.StudentName.Length >= other.StudentName.Length)
                return 1;
    
            return 0;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Student collection
            IList<Student> studentList = new List<Student>>() { 
                    new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
                    new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
                    new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
                    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
                    new Student() { StudentID = 5, StudentName = "Steve" , Age = 15 } 
                };
    
            var studentWithLongName = studentList.Max();
    
            Console.WriteLine("Student ID: {0}, Student Name: {1}", 
                                            .StudentID, studentWithLongName.StudentName)
        }
    }
     
    IList<int> intList = new List<int>() { 10, 21, 30, 45, 50, 87 };
    
    var total = intList.Sum();
    
    Console.WriteLine("Sum: {0}", total);
    
    var sumOfEvenElements = intList.Sum(i => {
                                        if(i%2 == 0)
                                            return i;
                
                                        return 0;
                                    });
    
    Console.WriteLine("Sum of Even Elements: {0}", sumOfEvenElements );
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } 
        };
    
    var sumOfAge = studentList.Sum(s => s.Age);
    
    Console.WriteLine("Sum of all student's age: {0}", sumOfAge);
            
    var numOfAdults = studentList.Sum(s => {
                
        if(s.Age >= 18)
            return 1;
        else
            return 0;
    });
     
    Console.WriteLine("Total Adult Students: {0}", numOfAdults);
  • 相关阅读:
    Vue框架(三)——Vue项目搭建和项目目录介绍、组件、路由
    Vue框架(二)——Vue指令(v-once指令、v-cloak指令、条件指令、v-pre指令、循环指令)、todolist案例、Vue实例(计算、监听)、组件、组件数据交互
    Vue框架(一)——Vue导读、Vue实例(挂载点el、数据data、过滤器filters)、Vue指令(文本指令v-text、事件指令v-on、属性指令v-bind、表单指令v-model)
    异步调用与回调机制
    进程池与线程池
    多线程实现并发的套接字通信
    线程queue
    定时器
    Event事件
    信号量
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6603108.html
Copyright © 2020-2023  润新知