public static void ExecuteCountMethodWithCondition() { int count = StudentData.GetStudentData().Count(a => a.Age > 20); "Count Method".PrintHeader(); Console.WriteLine("Number of Students whose age is above 20: {0}", count); }
public static void ExecuteAverageMethod() { IList <int> allAges = StudentData.GetStudentData().Select(a => a.Age).ToList(); "Average Method".PrintHeader(); Console.WriteLine("Average Age: {0}", allAges.Average()); }
public static void ExecuteAggregateMethodForAddition() { IList <Student> students = StudentData.GetStudentData(); "Aggregate Method".PrintHeader(); Console.WriteLine("Total Age: {0}", students.Aggregate(0, (totalAge, student) => totalAge += student.Age)); }
public static void ExecuteMinMaxMethod() { int maxAge = StudentData.GetStudentData().Max(a => a.Age); int minAge = StudentData.GetStudentData().Min(a => a.Age); "Use of Min-Max Methods".PrintHeader(); Console.WriteLine("Min Age: {0}", minAge); Console.WriteLine("Max Age: {0}", maxAge); }
public static void ExecuteAggregateMethodWithResult() { IList <Student> students = StudentData.GetStudentData(); string names = students.Aggregate("Names: ", (str, s) => str += s.Name + ",", str => str.Substring(0, str.Length - 1)); "Aggregate Method".PrintHeader(); Console.WriteLine(names); }
// Sum of the ages of students where age is a even number public static void ExecuteSumMethod() { int sum = StudentData.GetStudentData().Sum(a => { if (a.Age % 2 == 0) { return(a.Age); } else { return(0); } }); "Use of Sum Method with a condition".PrintHeader(); Console.WriteLine("Sum of Even Ages: {0}", sum); }
public static void ExecuteCountMethod() { int count = StudentData.GetStudentData().Count(); Console.WriteLine("Number of Students: {0}", count); }