static void Main()
    {
        StudentsDirectory    database1 = new StudentsDirectory();
        SpecialtiesDirectory database2 = new SpecialtiesDirectory();

        // LINQ query
        var joinedData =
            from student in database1.Students
            orderby student.FirstName ascending, student.LastName ascending
        join speciality in database2.StudentSpecialties on student.FacultyNumber equals speciality.FacultyNumber
            select new { student.FirstName, student.LastName, student.FacultyNumber, speciality.Speciality };

        // lambda expression
        var joinedDataLinqExtMtd =
            database1.Students.OrderBy(st => st.FirstName)
            .ThenBy(st => st.LastName)
            .Join(database2.StudentSpecialties, st => st.FacultyNumber, sp => sp.FacultyNumber,
                  (student, speciality) =>
                  new { student.FirstName, student.LastName, student.FacultyNumber, speciality.Speciality });

        // printing
        Console.WriteLine("{0, -18} {1, -8} {2, -12}", "Name", "FacNum", "Specialty");
        foreach (var item in joinedData)
        {
            Console.WriteLine("{0, -18} {1, -8} {2, -12}", item.FirstName + " " + item.LastName, item.FacultyNumber, item.Speciality);
        }
    }
Beispiel #2
0
        public static List <StudentWithGroup> CreateGroupsDirectory()
        {
            var studentsWithGroups = new List <StudentWithGroup>();

            // using the intitial students databae, to create new studets with groups database
            StudentsDirectory database = new StudentsDirectory();

            foreach (var student in database.Students)
            {
                if (student.GroupNumber == 1)
                {
                    studentsWithGroups.Add(new StudentWithGroup(student, "C#"));
                }
                else if (student.GroupNumber == 2)
                {
                    studentsWithGroups.Add(new StudentWithGroup(student, "PHP"));
                }
                else if (student.GroupNumber == 3)
                {
                    studentsWithGroups.Add(new StudentWithGroup(student, "JS"));
                }
                else if (student.GroupNumber == 4)
                {
                    studentsWithGroups.Add(new StudentWithGroup(student, "Java"));
                }
                else
                {
                    studentsWithGroups.Add(new StudentWithGroup(student, "No group chosen"));
                }
            }

            return(studentsWithGroups);
        }
Beispiel #3
0
        static void Main()
        {
            // creating an instance of the StudentsDirectory class, so that we can use its IList<Student>
            StudentsDirectory database = new StudentsDirectory();

            // running LINQ query
            var studentsByPhoneQuery = database.Students.StudentsByPhoneQuery();

            // printing - by invoking the PrintStudentsInfo() method from the StudentsByFirstAndLastName project
            StudentsByFirstAndLastName.PrintStudentsInfo(studentsByPhoneQuery);
        }
        static void Main()
        {
            // creating an instance of the StudentsDirectory class, so that we can use its IList<Student>
            StudentsDirectory database = new StudentsDirectory();

            // running LINQ query
            var studentsByNames = database.Students.StudentsByNames();

            // printing
            PrintStudentsInfo(studentsByNames);
        }
    static void Main()
    {
        // creating an instance of the StudentsDirectory class, so that we can use its IList<Student>
        StudentsDirectory database = new StudentsDirectory();

        // running LINQ query
        var studentsByGroupQuery =
            from student in database.Students
            orderby student.FirstName
            group student by student.GroupNumber
            into studentGroup
            where studentGroup.Key == 2
            select studentGroup;

        // printing
        PrintStudentInfoByGroup(studentsByGroupQuery);
    }
Beispiel #6
0
        static void Main()
        {
            // creating an instance of the StudentsDirectory class, so that we can use its IList<Student>
            StudentsDirectory database = new StudentsDirectory();

            // running LINQ query
            var excellentStudents =
                from student in database.Students
                where student.Marks.Contains(6)
                select new { Fullname = string.Join(" ", student.FirstName, student.LastName), Marks = string.Join(", ", student.Marks) };

            // printing
            foreach (var student in excellentStudents)
            {
                Console.WriteLine("Name: {0}, Grades: {{{1}}}", student.Fullname, student.Marks);
            }
        }
Beispiel #7
0
    static void Main()
    {
        // creating an instance of the StudentsDirectory class, so that we can use its IList<Student>
        StudentsDirectory database = new StudentsDirectory();

        // using lambda expressions
        var sortedStudents =
            database.Students.OrderByDescending(student => student.FirstName).ThenBy(student => student.LastName);

        // running LINQ query
        var studentsSortQuery = database.Students.StudentsSortQuery();

        // printing - by invoking the PrintStudentsInfo() method from the StudentsByFirstAndLastName project
        StudentsByFirstAndLastName.PrintStudentsInfo(sortedStudents);
        Console.WriteLine();
        StudentsByFirstAndLastName.PrintStudentsInfo(studentsSortQuery);
    }
Beispiel #8
0
    static void Main()
    {
        // creating an instance of the StudentsDirectory class, so that we can use its IList<Student>
        StudentsDirectory database = new StudentsDirectory();

        // running LINQ query
        var studentsByAgeQuery =
            from student in database.Students
            where student.Age >= 18 && student.Age <= 24
            select new { student.FirstName, student.LastName, student.Age }; // limiting the query to names and age

        // printing
        foreach (var student in studentsByAgeQuery)
        {
            Console.WriteLine("Name: {0} {1}, Age: {2}",
                              student.FirstName, student.LastName, student.Age);
        }
    }