public static void DescendingSortLambda(Student[] studentArr) { var sorted = studentArr.OrderByDescending(x => x.FirstName).ThenByDescending(y => y.LastName); foreach (var student in sorted) { Console.WriteLine(student.FirstName + " " + student.LastName); } }
public static void DescendingSortLINQ(Student[] studentArr) { var sorted = from student in studentArr orderby student.FirstName descending, student.LastName descending select student; foreach (var student in sorted) { Console.WriteLine(student.FirstName + " " + student.LastName); } }
//Methods with LINQ query operators public static void FindStudentByName(Student[] studentArr) { var findedStudents = from student in studentArr where student.FirstName.CompareTo(student.LastName) < 0 select student; int count = 0; foreach (var student in findedStudents) { Console.WriteLine(student.FirstName + " " + student.LastName); count++; } if (count == 0) { Console.WriteLine("There aren`t any students that satisfy the conditions"); } }
public static void FindStudentByAge(Student[] studentArr) { var find = from student in studentArr where student.Age > 17 && student.Age < 25 select student; int count = 0; foreach (var student in find) { Console.WriteLine(student.FirstName + " " + student.LastName); count++; } if (count == 0) { Console.WriteLine("There aren`t student between 18 and 24."); } }
static void Main() { Student[] studentArr = new Student[3]; studentArr[0] = new Student("Pesho", "Georgiev", 18); studentArr[1] = new Student("Ilian", "Ivanov", 24); studentArr[2] = new Student("Georgi", "Siromahov", 25); //Test methods Console.WriteLine("Students which first name is before its last name alphabetically:"); Student.FindStudentByName(studentArr); Console.WriteLine(); Console.WriteLine("Students between 18 and 24:"); Student.FindStudentByAge(studentArr); Console.WriteLine(); Console.WriteLine("Sorted students in descending order with Lambda expressions:"); Student.DescendingSortLambda(studentArr); Console.WriteLine(); Console.WriteLine("Sorted students in descending order with LINQ:"); Student.DescendingSortLINQ(studentArr); }