zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    SVN 怎么让文件脱离 版本控制
    WEB开发中使用和理解 .net中的认证与授权
    三层,师姐把我点透了
    三层与养猪,加入自己的理解。
    Asp.net的登录验证方法Web.config访问权限配置
    <%=%> 引发的aspx文件、.aspx.cs文件和.aspx.designer.cs的一些说明
    bin。obj Properties文件夹
    JS得到对应字段 的值。遍历
    C#中页面传值的方法。转载
    $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6603108.html
Copyright © 2011-2022 走看看