• 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);
  • 相关阅读:
    mybatis基础学习3---特殊sql语句(备忘)
    5.dos网络配置命令,重新获取ip刷新dns
    Guarded Suspension设计模式
    多线程Future设计模式
    多线程不可变对象设计模式immutable
    多线程读写锁分离设计模式
    多个人过门引出线程安全问题
    库存管理系统项目总结
    简单认识C#
    数据类型,变量,与运算符
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6603108.html
Copyright © 2020-2023  润新知