Search This Blog

Wednesday, July 20, 2011

Simple LINQ usage in C#

class Program
    {
        //define a list of strings
        private static List<string> people = new List<string>()
            {
              "Granville", "John", "Rachel", "Betty",
              "Chandler", "Ross", "Monica"
            };

        class Person
        {
            public string Name;
            public int height;
        }

        //define a list of Person objects
        private static List<Person> people2 = new List<Person>()
        {
            new Person{Name = "Granville", height = 165},
            new Person{Name = "John", height = 185},
                new Person{Name = "Rachel", height = 179},
                    new Person{Name = "Betty", height = 125},
            new Person{Name = "Chandler", height = 132},
            new Person{Name = "Ross", height = 153},
            new Person{Name = "Monica" , height = 198}
        };


        //list items in people = List<Person> which height's is greater than 100
        public static void Example4()
        {
            IEnumerable<Person> query = from p in people2 where p.height > 180 select p;

            foreach (Person person in query)
            {
                Console.WriteLine(person.Name + " " + person.height);
            }
        }

        //list items in people = List<Person> which height's is greater than 100 and order by this Name ascending
        //using lambda expression
        public static void Example5()
        {
            IEnumerable<Person> query = people2.Where(x => x.height > 180).OrderBy(x => x.Name);

            foreach (Person person in query)
            {
                Console.WriteLine(person.Name + " " + person.height);
            }
        }

        //count items in people = List<string>
        public static void Example1()
        {
            int queryCount = (from p in people select p).Count();
            Console.WriteLine(queryCount);
        }

        //list items in people = List<string> which string's length is greater than 5
        public static void Example2()
        {
            IEnumerable<string> query = from p in people
                                        where p.Length > 5
                                        orderby p
                                        select p;

            foreach (string person in query)
            {
                Console.WriteLine(person);
            }
        }

        //list items in people = List<string> which string's length is greater than 5 and order by this string ascending
        //using lambda expressions
        public static void Example3()
        {
            IEnumerable<string> query =  people.Where(x => x.Length > 5).OrderBy(x => x);

            foreach (string person in query)
            {
                Console.WriteLine(person);
            }
        }

       

        static void Main(string[] args)
        {
            //Example1();
            //Example2();
            //Example3();
            //Example4();
            Example5();

            Console.ReadLine();
        }
    }

1 comment:

If you like this post, please leave a comment :)