This weeks session was on LINQ and Silverlight.

LINQ

The LINQ session was a pretty basic roundup of some of the most common extension methods that you use with LINQ. Try filling out the blanks in the following code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>
            {
                new Employee { FirstName = "AAA", LastName = "BBB", Age = 24, Weight = 75, Height = 1.78, Gender = Gender.Male },
                new Employee { FirstName = "CCC", LastName = "DDD", Age = 24, Weight = 75, Height = 1.76, Gender = Gender.Male },
                new Employee { FirstName = "EEE", LastName = "FFF", Age = 40, Weight = 110, Height = 1.78, Gender = Gender.Male },
                new Employee { FirstName = "GGG", LastName = "HHH", Age = 39, Weight = 93, Height = 1.93, Gender = Gender.Male },
                new Employee { FirstName = "III", LastName = "JJJ", Age = 24, Weight = 76, Height = 1.78, Gender = Gender.Male },
                new Employee { FirstName = "KKK", LastName = "LLL", Age = 29, Weight = 65, Height = 1.57, Gender = Gender.Female },
                new Employee { FirstName = "MMM", LastName = "NNN", Age = 34, Weight = 103.5, Height = 1.73, Gender = Gender.Male },
                new Employee { FirstName = "OOO", LastName = "PPP", Age = 25, Weight = 77, Height = 1.86, Gender = Gender.Male },
                new Employee { FirstName = "QQQ", LastName = "RRR", Age = 35, Weight = 82, Height = 1.8, Gender = Gender.Male },
                new Employee { FirstName = "SSS", LastName = "TTT", Age = 34, Weight = 103.5, Height = 1.73, Gender = Gender.Male },
                new Employee { FirstName = "UUU", LastName = "VVV", Age = 26, Weight = 90, Height = 1.74, Gender = Gender.Male },
                new Employee { FirstName = "WWW", LastName = "XXX", Age = 34, Weight = 79, Height = 1.77, Gender = Gender.Male },
                new Employee { FirstName = "YYY", LastName = "ZZZ", Age = 26, Weight = 90.5, Height = 1.72, Gender = Gender.Male },
                new Employee { FirstName = "111", LastName = "222", Age = 34, Weight = 52, Height = 1.58, Gender = Gender.Female},
                new Employee { FirstName = "333", LastName = "444", Age = 34, Weight = 90.5, Height = 1.76, Gender = Gender.Male }
            };

            // Find the average Weight
            Console.WriteLine("Average Weight:\t{0}", null);

            // Find the average age
            Console.WriteLine("Average Age:\t{0}", null);
            
            // Find the average BMI
            Console.WriteLine("Average BMI:\t{0}", null);

            // Find the number of females
            Console.WriteLine("Females:\t{0}", null);

            // Find the number of males
            Console.WriteLine("Males:\t{0}", null);

            // Is anyone a minor
            Console.WriteLine("Do we have minors: {0}", null);

            // Does everyone have a firstname longer than 3 characters
            Console.WriteLine("Does everyone have a firstname longer than 3 characters: {0}", null);
            
            // Create a long string of employee initials
            Console.WriteLine("String of employee initials: {0}", 
                null);
   
            // Group by genders and print the average height, weight and BMI
            var genderGroups = null

            foreach (var e in genderGroups)
            {
                Console.WriteLine("Averages for {0}s", e.Key.ToString());
                Console.WriteLine("Weight:\t{0}", null);
                Console.WriteLine("Height:\t{0}", null);
                Console.WriteLine("BMI:\t{0}", null);
                
            }

            Console.ReadKey();
        }
    }

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public double Weight { get; set; }
        public double Height { get; set; }
        public string Name { get { return string.Format("{0} {1}", FirstName, LastName); } }
        public Gender Gender { get; set; }
    }

    public enum Gender
    {
        Female,
        Male
    }
}

Solutions at the bottom (don’t peak!)

Silverlight

For silverlight we checked out an awesome demo financial application from the Microsoft Silverlight Showcase

LINQ Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>
            {
                new Employee { FirstName = "AAA", LastName = "BBB", Age = 24, Weight = 75, Height = 1.78, Gender = Gender.Male },
                new Employee { FirstName = "CCC", LastName = "DDD", Age = 24, Weight = 75, Height = 1.76, Gender = Gender.Male },
                new Employee { FirstName = "EEE", LastName = "FFF", Age = 40, Weight = 110, Height = 1.78, Gender = Gender.Male },
                new Employee { FirstName = "GGG", LastName = "HHH", Age = 39, Weight = 93, Height = 1.93, Gender = Gender.Male },
                new Employee { FirstName = "III", LastName = "JJJ", Age = 24, Weight = 76, Height = 1.78, Gender = Gender.Male },
                new Employee { FirstName = "KKK", LastName = "LLL", Age = 29, Weight = 65, Height = 1.57, Gender = Gender.Female },
                new Employee { FirstName = "MMM", LastName = "NNN", Age = 34, Weight = 103.5, Height = 1.73, Gender = Gender.Male },
                new Employee { FirstName = "OOO", LastName = "PPP", Age = 25, Weight = 77, Height = 1.86, Gender = Gender.Male },
                new Employee { FirstName = "QQQ", LastName = "RRR", Age = 35, Weight = 82, Height = 1.8, Gender = Gender.Male },
                new Employee { FirstName = "SSS", LastName = "TTT", Age = 34, Weight = 103.5, Height = 1.73, Gender = Gender.Male },
                new Employee { FirstName = "UUU", LastName = "VVV", Age = 26, Weight = 90, Height = 1.74, Gender = Gender.Male },
                new Employee { FirstName = "WWW", LastName = "XXX", Age = 34, Weight = 79, Height = 1.77, Gender = Gender.Male },
                new Employee { FirstName = "YYY", LastName = "ZZZ", Age = 26, Weight = 90.5, Height = 1.72, Gender = Gender.Male },
                new Employee { FirstName = "111", LastName = "222", Age = 34, Weight = 52, Height = 1.58, Gender = Gender.Female},
                new Employee { FirstName = "333", LastName = "444", Age = 34, Weight = 90.5, Height = 1.76, Gender = Gender.Male }
            };

            // Find the average Weight
            Console.WriteLine("Average weight:\t{0}", employees.Average(e => e.Weight));

            // Find the average age
            Console.WriteLine("Average age:\t{0}", employees.Average(e => e.Age));

            // Find the average BMI (weight / height^2)
            Console.WriteLine("Average bmi:\t{0}", employees.Average(e => (e.Weight / Math.Pow(e.Height,2 ))));

            // Find the number of females
            Console.WriteLine("Number of Females:\t{0}", employees.Count(e => e.Gender == Gender.Female));

            // Find the number of males
            Console.WriteLine("Number of Males:\t{0}", employees.Where(e => e.Gender == Gender.Male).Count());

            // Is anyone a minor
            Console.WriteLine("Is anyone a minor:\t {0}", employees.Any(e => e.Age < 18) ? "Y" : "N");

            // Does everyone have a firstname longer than 3 characters
            Console.WriteLine("Does everyone have a firstname longer than 3 characters:\t {0}", employees.All(e => e.FirstName.Length > 3) ? "Y" : "N");

            // Create a long string of employee initials
            Console.WriteLine("Initials:\t {0}", 
                string.Join(",",
                    employees.Select(e => string.Format("{0}{1}", e.FirstName[0], e.LastName[0])).ToArray()));

            // Using aggregates
            Console.WriteLine("Initials:\t {0}", 
                employees.Aggregate(
                    string.Empty,
                    (collection, e) => string.Format("{0}{1}{2},", collection, e.FirstName[0], e.LastName[0])
                    )
                );

            // Group by genders and print averages
            var results = employees.GroupBy(
                g => g.Gender,
                e => e);

            foreach (var gender in results)
            {
                Console.WriteLine("Averages for '{0}'", gender.Key);
                Console.WriteLine("\tWeight:\t {0}", gender.Average(e => e.Weight));
                Console.WriteLine("\tHeight:\t {0}", gender.Average(e => e.Height));
                Console.WriteLine("\tBMI:\t {0}", gender.Average(e => e.Weight / Math.Pow(e.Height, 2)));
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public double Weight { get; set; }
        public double Height { get; set; }
        public string Name { get { return string.Format("{0} {1}", FirstName, LastName); } }
        public Gender Gender { get; set; }
    }

    public enum Gender
    {
        Female,
        Male
    }
}

Resources

Comment