zoukankan      html  css  js  c++  java
  • 运用Linq操作对象示例

    Java和C++对于容器中元素的操作已经十分简便智能了,但与C#相比,简直弱爆了。这正是因为C#拥有一个利器:Linq。简单的说,Linq就是利用类SQL语句来操作容器中的元素。这样就完全放弃了编程的思维方式,而采用浅显易懂的数据库操作了。这不得不说是一次重大的突破,不过同时也让C#程序员越来越懒,失去了程序员的血性。

    下面简单举个例子,说明Linq的用法。

    1、定义一个Person类,包括ID,Nam,Age三个属性。

    class Person
    {
        public Person(int id, string name, int age)
        {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        private int id;
    
        // Person ID
        public int PersonID
        {
            get { return this.id; }
            set { this.id = value; }
        }
    
        private string name;
    
        // Person name
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
    
        public int age;
    
        // Age that ranges from 1 to 100
        public int Age
        {
            get { return this.age; }
            set
            {
                if (value > 0 && value <= 100)
                    this.age = value;
                else
                    throw new Exception("Age is out of scope [1,100]");
            }
        }
    }

    2、创建一个List<Person>作为数据源

    // Build the Person List as data source
    List<Person> persons = new List<Person>();
    
    persons.Add(new Person(1, "Alexander David", 20));
    persons.Add(new Person(2, "Aziz Hassouneh", 18));
    persons.Add(new Person(3, "Guido Pica", 20));
    persons.Add(new Person(4, "Chris Preston", 19));
    persons.Add(new Person(5, "Jorgen Rahgek", 20));
    persons.Add(new Person(6, "Todd Rowe", 18));
    persons.Add(new Person(7, "SPeter addow", 22));
    persons.Add(new Person(8, "Markus Breyer", 19));
    persons.Add(new Person(9, "Scott Brown", 20));

    3、查询

    // query a person in the data source.
    var Todd = (from p in persons
                where p.Name == "Todd Rowe"
                select p).First();
    Console.WriteLine("Todd Rowe's age is {0}", Todd.Age);

    4、更新

    // update person's age
    Todd.Age = 21;
    Console.WriteLine("Todd Rowe's age is updated to {0}", (from p in persons
                                                        where p.Name == "Todd Rowe"
                                                        select p).First().Age);

    5、排序并输出

    // sort by person's age
    var query1 = from p in persons
                    orderby p.Age
                    select p;
    Console.WriteLine("ID\tName\t\t\tAge");
    foreach (Person p in query1.ToList<Person>())
    {
        Console.WriteLine("{0}\t{1}\t\t{2}", p.PersonID, p.Name, p.Age);
    }

    6、获取平均值、最大值、最小值

    // avgerage, max, min age of the persons
    double avgAge = (from p in persons
                        select p.Age).Average();
    Console.WriteLine("The average age of the persons is {0:f2}", avgAge);
    
    double maxAge = (from p in persons
                        select p.Age).Max();
    Console.WriteLine("The Max age of the persons is {0}", maxAge);
    
    double minAge = (from p in persons
                        select p.Age).Min();
    Console.WriteLine("The min age of the persons is {0}", minAge);

    7、特定条件查询(获取20岁以上的人名)

    // Count the persons who age is larger than 20
    var query2 = from p in persons
                    where p.Age > 20
                    select p;
    
    int count = query2.Count();
    Console.WriteLine("{0} persons are older than 20:", count);
    for (int i = 0; i < count; i++)
    {
        Console.WriteLine(query2.ElementAt(i).Name);
    }

    输出结果:

    image

    上述示例,参考了MSDN。

    作者:pezy 出处:http://www.cnblogs.com/pezy 欢迎转载,也请保留这段声明。谢谢!
  • 相关阅读:
    [bzoj1911][Apio2010特别行动队] (动态规划+斜率优化)
    [bzoj1597][usaco2008 mar]土地购买 (动态规划+斜率优化)
    [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
    整体二分初步
    bzoj[3238][ahoi差异]
    概率dp学习
    poj[2104]K-th Number
    hdu[1711]number sequence
    hdu[2222]keywords search
    poj[1187][Noi 01]陨石的秘密
  • 原文地址:https://www.cnblogs.com/pezy/p/2417504.html
Copyright © 2011-2022 走看看