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);
  • 相关阅读:
    c++11 内存模型解读
    无锁队列的实现
    c++中的原子操作
    还是说Memory Model,gcc的__sync_synchronize真是太坑爹了
    对于Linux平台下C语言开发中__sync_函数的认识
    理解 Memory barrier
    pthread_barrier_init,pthread_barrier_wait简介
    explicit构造函数的作用
    droofs
    27.
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6603108.html
Copyright © 2011-2022 走看看