LINQ to Objects Demo
Using the Code 1. Define the Person class that includes the ID, Name, and Age properties. class Person { public Person(int id, string name, int age) { this.id = id; this.name = name; this.age = age; } private int id; /// <summary> /// Person ID /// </summary> public int PersonID { get { return this.id; } set { this.id = value; } } private string name; /// <summary> /// Person name /// </summary> public string Name { get { return this.name; } set { this.name = value; } } private int age; /// <summary> /// Age that ranges from 1 to 100 /// </summary> 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. Build a list of Person to be used as the data source. ///////////////////////////////////////////////////////////////////// // Build the Person list that serves as the 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. Use Linq to perform the query operation. ///////////////////////////////////////////////////////////////////// // Query a person in the data source. // var Todd = (from p in persons where p.Name == "Todd Rowe" select p).First(); // [-or-] // Use extension method + lambda expression directly //var Todd = persons.Where(p => p.Name == "Todd Rowe").First(); Console.WriteLine("Todd Rowe's age is {0}", Todd.Age); 4. Use Linq to perform the Update operation. ///////////////////////////////////////////////////////////////////// // Perform the Update operation on the 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. Use Linq to perform the Order operation. ///////////////////////////////////////////////////////////////////// // Sort the data in the data source. // Order the persons by age var query1 = from p in persons orderby p.Age select p; Console.WriteLine("ID\tName\t\tAge"); foreach (Person p in query1.ToList<Person>()) { Console.WriteLine("{0}\t{1}\t\t{2}", p.PersonID, p.Name, p.Age); } 6. Use Linq to perform the Max, Min, Average queries. ///////////////////////////////////////////////////////////////////// // Print the average, 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 maximum age of the persons is {0}", maxAge); double minAge = (from p in persons select p.Age).Min(); Console.WriteLine("The minimum age of the persons is {0}", minAge); 7. Use Linq to count the Person whose age is larger than 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); }