Exemple #1
0
        public static void PrintStudentName(Student3 std)
        {
            if (std == null)
            {
                throw new NullReferenceException("Student object is null. ");
            }

            Console.WriteLine(std.StudentName);
        }
        static void  Main1()
        {
            //The Linq Query to Get All the Students--------------------------------------------------------------------------------------------------------------------
            Console.WriteLine("Before Sorting:");
            IEnumerable <Student3> students0 = Student3.GetAllStudetns();

            foreach (Student3 stu in students0)
            {
                Console.WriteLine(stu.TotalMarks + "\t" + stu.Name + "\t" + stu.StudentID);
            }
            Console.WriteLine();

            //+++++++++++++++++++++++++++++++++++++++++++++++++Sorting By Using Primary and Secondary Sort +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //a) Sorts Students first by TotalMarks in ascending order(Primary Sort)
            //b) The 4 Students with TotalMarks of 800, will then be sorted by Name in ascending order(First Secondary Sort)
            //c) The 2 Students with Name of John, will then be sorted by StudentID in ascending order(Second Secondary Sort)
            Console.WriteLine("Sorted Order by using Linq:");
            IOrderedEnumerable <Student3> students1 = Student3.GetAllStudetns().OrderBy(stu => stu.TotalMarks).ThenBy(stu => stu.Name).ThenByDescending(stu => stu.StudentID);

            foreach (Student3 stu in students1)
            {
                Console.WriteLine(stu.TotalMarks + "\t" + stu.Name + "\t" + stu.StudentID);
            }
            Console.WriteLine();

            //Implementation of above Query by Using Sql Like Query-----------------------------------------------------------------------------------------------------
            Console.WriteLine("Sorted Order by using Sql Like Queries:");
            IOrderedEnumerable <Student3> students2 = from stu in Student3.GetAllStudetns()
                                                      orderby stu.TotalMarks, stu.Name, stu.StudentID descending
            select stu;

            foreach (Student3 stu in students2)
            {
                Console.WriteLine(stu.TotalMarks + "\t" + stu.Name + "\t" + stu.StudentID);
            }
            Console.WriteLine();

            //Reversing of the Collection of Student--------------------------------------------------------------------------------------------------------------------
            Console.WriteLine("Before Revsering:");
            IEnumerable <Student3> students3 = Student3.GetAllStudetns();

            foreach (Student3 stu in students3)
            {
                Console.WriteLine(stu.StudentID + "\t" + stu.Name + "\t" + stu.TotalMarks);
            }
            Console.WriteLine();

            Console.WriteLine("After Revsering:");
            IEnumerable <Student3> result0 = students3.Reverse();

            foreach (Student3 stu in result0)
            {
                Console.WriteLine(stu.StudentID + "\t" + stu.Name + "\t" + stu.TotalMarks);
            }
        }
Exemple #3
0
        public static void Main()
        {
            Student3 std = null;

            try
            {
                PrintStudentName(std);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }