// Task 5 - the LINQ part - by first and last names in descending orders public static Student[] OrderByNames(Student[] arr) { var ordered = from student in arr orderby student.FirstName descending, student.LastName descending select student; Student[] array = ordered.ToArray(); return array; }
// 3 Task - students with first name before last public static Student[] FirstBeforeLast(Student[] arr) { var firstBeforeLast = from student in arr where student.FirstName.CompareTo(student.LastName) == -1 select student; Student[] array = firstBeforeLast.ToArray(); return array; }
// 4 Task - students between 18 and 24 years old public static Student[] AgeInterval(Student[] arr) { var ageInterval = from student in arr where ((student.Age >= 18) && (student.Age <= 24)) select student; Student[] array = ageInterval.ToArray(); return array; }
public Student GetStudent() { Student Su = new Student(); DataTable dt = new DataTable(); dt = Db.TruyVan_TraVe_DataTable("select * from nguoihoc"); Su.Ma = Int32.Parse(dt.Columns[1].ToString()); Su.Ten = dt.Columns[2].ToString(); Su.Mk = dt.Columns[2].ToString(); Su.Dt = dt.Columns[2].ToString(); Su.Sn = dt.Columns[2].ToString(); return Su; }
static void Main() { // Test StringBuilder try { StringBuilder str = new StringBuilder("Telerik Academy Students"); Console.WriteLine(str.Substring(2)); Console.WriteLine(str.Substring(2, 4)); Console.WriteLine(); } catch (Exception e) { Console.WriteLine(e.Message); } // Test IEnumerable extensions int[] arr = new int[4] { 4, -23, 11, 3 }; Console.WriteLine("The min value is {0}", arr.MaxValue<int>()); Console.WriteLine("The max value is {0}", arr.MinValue<int>()); Console.WriteLine("The sum is {0}", arr.Sum<int>()); Console.WriteLine("The product is {0}", arr.Product<int>()); Console.WriteLine("The average is {0}", arr.Average<int>()); Console.WriteLine(); // Test LINQ and Lambda functions // 3 task test Student[] std = new Student[5]{ new Student("Acho","Georgieva",22), new Student("Acho","Georgiev",22), new Student("Galen", "Asenov",16), new Student("Anton", "Antonov", 40), new Student("Milko","Manchev",34) }; Student[] studentArr = LINQmethods.FirstBeforeLast(std); Console.WriteLine("FirstBeforeLast:"); foreach (var c in studentArr) Console.WriteLine(c); Console.WriteLine(); // 4 task test studentArr = LINQmethods.AgeInterval(std); Console.WriteLine("Age Between 18 and 24:"); foreach (var c in studentArr) Console.WriteLine(c); Console.WriteLine(); // 5 task test - first the lambda function var ordered = std.OrderByDescending(student => student.FirstName).ThenByDescending(student => student.LastName); Console.WriteLine("Ordered by name descending - lambda function:"); foreach (var c in ordered) Console.WriteLine(c); Console.WriteLine(); // Then with LINQ studentArr = LINQmethods.OrderByNames(std); Console.WriteLine("Ordered by name descending - LINQ:"); foreach (var c in studentArr) Console.WriteLine(c); Console.WriteLine(); int[] array = new int[7] { 2, 3, 5, 7, 21, 42, 43 }; // 6 task - testing first with lambda expressions var divNumbers = Array.FindAll(array, divNumber => ((divNumber % 3 == 0) && (divNumber % 7 == 0))); // or one check - divNumber%21 Console.WriteLine("Divisible integers - first with lambda expressions."); foreach (int c in divNumbers) Console.WriteLine(c); Console.WriteLine(); Console.WriteLine("Divisible integers - now with LINQ."); int[] intArray = LINQmethods.DivisibleNumbers(array); foreach (var c in intArray) Console.WriteLine(c); Console.WriteLine(); // Task 7 - create a timer delegate that can call a function in a specified interval. // You have to use the constructor for the class - it takes the function and the // interval in seconds - the interval must be non-negative. Console.WriteLine("Call a function using only a delegate:"); TimerWithDelegate del = new TimerWithDelegate(FunctionToTest, 1); // Executing the function a specific number of times by calling the Execute Timer method with // a parameter the number of executions del.ExecuteTimer(5); Console.WriteLine(); // 8 Task - using events. First we create an instance of our timer class containing the timer interval // and the number of function executions. Console.WriteLine("Call a function with an event:"); TimerWithEvent eventTime = new TimerWithEvent(1, 5); // Then we create an instance of the subscriber by specifying the function to be executed and the timer to // subscribe to. SubscriberToTimerWithEvent sub = new SubscriberToTimerWithEvent(FunctionToTest, eventTime); // Start execution. eventTime.StartExecution(); eventTime.StartExecution(); }