Beispiel #1
0
 public static Students[] SortStudentsUsingLINQ(Students[] arr)
 {
     var result = from x in arr
                  orderby x.FirstName descending, x.LastName descending
                  select x;
     return result.ToArray();
 }
Beispiel #2
0
 public static Students[] SortStudentsByAge(Students[] arr)
 {
     var result = from x in arr
                  where x.Age <= 24 && x.Age >= 18
                  select x;
     return result.ToArray();
 }
Beispiel #3
0
 public static Students[] SortStudentsByName(Students[] arr)
 {
     var result = from x in arr
                  where x.FirstName.CompareTo(x.LastName) < 0
                  select x;
     return result.ToArray();
 }
Beispiel #4
0
 //method to print Students[] array
 public static string ToString(Students[] arr)
 {
     StringBuilder sb = new StringBuilder();
     foreach (var item in arr)
     {
         sb.AppendLine(item.FirstName
             + ", " 
             + item.LastName 
             + ", " 
             + item.Age);
     }
     return sb.ToString();
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            //anonymous type
            var arr = new Students[]
            {
                new Students {FirstName = "Gosho", LastName = "Petrov", Age = 15},
                new Students {FirstName = "Pesho", LastName = "Cvetkov", Age = 19},
                new Students {FirstName = "Misho", LastName = "Ivanov", Age = 24}
            };

            Console.WriteLine(ToString(SortStudentsByName(arr)));
            Console.WriteLine(ToString(SortStudentsByAge(arr)));
            Console.WriteLine(ToString(SortStudentsUsingLambda(arr)));
            Console.WriteLine(ToString(SortStudentsUsingLINQ(arr))); 
        }
Beispiel #6
0
 public static Students[] SortStudentsUsingLambda(Students[] arr)
 {
     var result = arr.OrderByDescending(x => x.FirstName).
                 ThenByDescending(x => x.LastName).ToArray();
     return result.ToArray();
 }