Exemplo n.º 1
0
        private static void task2(StudentSystemContext context)
        {
            var courses = context.Courses.Select(c => new
            {
                Name = c.Name,
                Description = c.Description,
                Resources = c.Resources.Select(r => new
                {
                    Name = r.Name,
                    Type = r.TypeOfResource,
                    URL = r.URL
                })
            });

            foreach (var course in courses)
            {
                Console.WriteLine(course.Name);
                Console.WriteLine(course.Description);

                var courseResources = course.Resources.ToList();
                foreach (var resource in courseResources)
                {
                    Console.WriteLine(resource.Name + " / " + resource.Type + " / " + resource.URL);
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 2
0
        public void ImportCourses(int numberOfCourses)
        {
            var db = new StudentSystemContext();
            Console.Write("Importing Courses");

            for (int i = 0; i < numberOfCourses; i++)
            {
                var course = new Course()
                {
                    Name = RandomGenerator.GetString(10, 100),
                    Description = RandomGenerator.GetString(100, 1000)
                };

                db.Courses.Add(course);

                if (i % 50 == 0)
                {
                    Console.Write(".");
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentSystemContext();
                }
            }

            db.SaveChanges();
            Console.WriteLine(" Completed!");
        }
        private static void ListStudentsAndTheirSubmissions(StudentSystemContext ctx)
        {
            var students = ctx.Students
                .Select(s => new
                {
                    s.Name,
                    Homeworks = s.Homeworks.Select(h => new
                    {
                        h.Content,
                        h.ContentType
                    })
                });

            foreach (var student in students)
            {
                Console.WriteLine("--" + student.Name);

                foreach (var homework in student.Homeworks)
                {
                    Console.WriteLine(homework.Content + "; " + homework.ContentType);
                }

                Console.WriteLine();
            }
        }
Exemplo n.º 4
0
        public static void Main()
        {
            var context = new StudentSystemContext();

            //Problem 3 Solutions
            //task1(context);
            //task2(context);
            //task3(context);
            //task4(context);
            task5(context);
        }
Exemplo n.º 5
0
        public static void ListAllCourses()
        {
            using (var db = new StudentSystemContext())
            {
                var courses = db.Courses;

                foreach (var course in courses)
                {
                    Console.WriteLine("{0} - {1}; Length {2} - {3}; Price: {4}", course.Name, course.Description,
                    course.StartDate, course.EndDate, course.Price);
                }
            }
        }
Exemplo n.º 6
0
        public static void AddHomework(string contentUrl, FileType fileType, DateTime date)
        {
            using (var db = new StudentSystemContext())
            {
                db.Homeworks.Add(new Homework
                {
                    ContentUrl = contentUrl,
                    FileType = fileType,
                    Date = date
                });

                db.SaveChanges();
            }
        }
Exemplo n.º 7
0
        public static void AddResource(string name, string link, ResourceType resourceType = ResourceType.Other)
        {
            using (var db = new StudentSystemContext())
            {
                db.Resources.Add(new Resource
                {
                    Name = name,
                    Link = link,
                    ResourceTypes = resourceType
                });

                db.SaveChanges();
            }
        }
Exemplo n.º 8
0
        public static void AddCourse(string name, string desctiption, DateTime startDate, DateTime endDate, decimal price = 0.0m)
        {
            using (var db = new StudentSystemContext())
            {
                db.Courses.Add(new Course
                {
                    Name = name,
                    Description = desctiption,
                    StartDate = startDate,
                    EndDate = endDate,
                    Price = price
                });

                db.SaveChanges();
            }
        }
Exemplo n.º 9
0
        public static void AddStudent(string firstName, string lastName,
            DateTime birthDate, string phoneNumber = null)
        {
            using (var db = new StudentSystemContext())
            {
                db.Students.Add(new Student
                {
                    FirstName = firstName,
                    LastName = lastName,
                    BirthDate = birthDate,
                    PhoneNumber = phoneNumber,
                    RegistrationDate = DateTime.Now
                });

                db.SaveChanges();
            }
        }
        private static void ListCoursesWithMoreThan3Students(StudentSystemContext ctx)
        {
            var courses = ctx.Courses
                .Where(c => c.Students.Count > 3)
                .OrderByDescending(c => c.Resources.Count)
                .ThenByDescending(c => c.StartDate)
                .Select(c => new
                {
                    c.Name,
                    ResourceCount = c.Resources.Count
                });

            foreach (var course in courses)
            {
                Console.WriteLine("--" + course.Name);
                Console.WriteLine("Resource count: " + course.ResourceCount);
            }
        }
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>());

            using (var db = new StudentSystemContext())
            {
                var student = new Student { Name = "Ivan Bojinov", Number = "987654321" };

                db.Students.Add(student);

                var course = new Course { Name = "C# Intermediate", Description = "Writing more advanced programs in C#" };

                db.Courses.Add(course);

                db.SaveChanges();
            }

            Console.WriteLine("Saved!");
        }
        public static void Main()
        {
            var ctx = new StudentSystemContext();

            ListStudentsAndTheirSubmissions(ctx);
            Console.WriteLine(new string('-', 70));

            ListCoursesWithResources(ctx);
            Console.WriteLine(new string('-', 70));

            ListCoursesWithMoreThan3Students(ctx);
            Console.WriteLine(new string('-', 70));

            ListActiveCoursesOnAGivenDate(ctx);
            Console.WriteLine(new string('-', 70));

            ListStudentsNumberOfCoursesAndPrice(ctx);
            Console.WriteLine(new string('-', 70));
        }
Exemplo n.º 13
0
        private static void task3(StudentSystemContext context)
        {
            var courses = context.Courses
                .Where(c => c.Resources.Count > 3)
                .OrderBy(c => c.Resources.Count)
                .ThenBy(c => c.StartDate)
                .ThenByDescending(c => c.EndDate)
                .Select(c => new
                {
                    Name = c.Name,
                    Resorces = c.Resources.Count
                });


            foreach (var course in courses)
            {
                Console.WriteLine(course.Name + " Resources: " + course.Resorces);
            }
        }
Exemplo n.º 14
0
        public void ImportHomeWorks(int numberOfHomeworks)
        {
            Console.Write("Importing Homework");
            var db = new StudentSystemContext();
            var studentIds = db.Students.Select(s => s.Id).ToList();
            var studentsCount = studentIds.Count();
            var counter = 0;

            while (numberOfHomeworks > 0)
            {
                var student = db.Students.Find(studentIds[RandomGenerator.GetInt(0, studentsCount - 1)]);
                if (student != null && student.Courses.Count != 0)
                {
                    var homework = new Homework()
                    {
                        Content = RandomGenerator.GetString(20, 1000),
                        TimeSent = RandomGenerator.GetDate(new DateTime(2014, RandomGenerator.GetInt(1, 12), RandomGenerator.GetInt(1, 28)), DateTime.Now)
                    };

                    student.Homeworks.Add(homework);
                    var studentCoursesIds = student.Courses.Select(c => c.Id).ToList();

                    db.Courses.Find(studentCoursesIds[RandomGenerator.GetInt(0, studentCoursesIds.Count - 1)]).Homeworks.Add(homework);
                    counter++;
                    numberOfHomeworks--;
                }

                if (counter > 100)
                {
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentSystemContext();
                    Console.Write(".");
                    counter = 0;
                }
            }

            db.SaveChanges();
            Console.WriteLine(" Completed!");
        }
Exemplo n.º 15
0
        public static void Main()
        {
            Database.SetInitializer(
                new MigrateDatabaseToLatestVersion
                <StudentSystemContext, Configuration>());

            var db = new StudentSystemContext();

            var student = new Student
            {
                FirstName = "Ivan",
                LastName = "Ivanov",
                Number = 13532
            };

            var course = new Course
            {
                Name = "JS",
                Description = "JS",
                Materials = "www.telerik"
            };

            var homework = new Homework
            {
                Content = "Done",
                TimeSent = DateTime.Now,
                StudentId = 2,
                CourseId = 2
            };

            db.Homeworks.Add(homework);
            student.Courses.Add(course);
            db.Students.Add(student);
            db.Courses.Add(course);
            db.SaveChanges();

            Console.WriteLine(db.Students.Count());
            Console.WriteLine(db.Courses.Count());
        }
Exemplo n.º 16
0
        public void ImportMaterials(int averageMaterialsInCourse)
        {
            var db = new StudentSystemContext();
            Console.Write("Importing Materials");

            var courseIds = db.Courses.Select(c => c.Id).ToList();
            var counter = 0;
            foreach (var id in courseIds)
            {
                var numberOfMaterialsForCurrentCourse = RandomGenerator
                    .GetInt(averageMaterialsInCourse - 5, averageMaterialsInCourse + 5);

                for (int j = 0; j < numberOfMaterialsForCurrentCourse; j++)
                {
                    var material = new Material()
                    {
                        Name = RandomGenerator.GetString(10, 80),
                        Content = RandomGenerator.GetString(100, 3000),
                        CourseId = id
                    };

                    db.Materials.Add(material);
                    counter++;
                }

                if (counter > 100)
                {
                    Console.Write(".");
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentSystemContext();
                    counter = 0;
                }
            }

            db.SaveChanges();
            Console.WriteLine(" Completed!");
        }
Exemplo n.º 17
0
        private static void task1(StudentSystemContext context)
        {
            var students = context.Students.Select(s => new
            {
                Name = s.Name,
                Homeworks = s.Homeworks.Select(h => new
                {
                    Content = h.Content,
                    ContentType = h.ContentType
                })
            });

            foreach (var student in students)
            {
                Console.WriteLine(student.Name);

                var studentHomeworks = student.Homeworks.ToList();
                foreach (var homework in studentHomeworks)
                {
                    Console.WriteLine(homework.Content + "/" + homework.ContentType);
                }
                Console.WriteLine();
            }
        }
        private static void ListCoursesWithResources(StudentSystemContext ctx)
        {
            var courses = ctx.Courses
                .OrderBy(c => c.StartDate)
                .ThenByDescending(c => c.EndDate)
                .Select(c => new
                {
                    c.Name,
                    c.Description,
                    c.Resources
                });

            foreach (var course in courses)
            {
                Console.WriteLine("--" + course.Name);

                foreach (var res in course.Resources)
                {
                    Console.WriteLine(res.Name + "; " + res.Type + "; " + res.URL);
                }

                Console.WriteLine();
            }
        }
        private static void ListActiveCoursesOnAGivenDate(StudentSystemContext ctx)
        {
            var activeOn = new DateTime(2014, 2, 5);
            var courses = ctx.Courses
                .Where(c => c.StartDate < activeOn && c.EndDate > activeOn)
                .ToList()
                .Select(c => new
                {
                    c.Name,
                    c.StartDate,
                    c.EndDate,
                    Duration = (c.EndDate - c.StartDate).Days,
                    StudentsCount = c.Students.Count
                })
                .OrderByDescending(c => c.StudentsCount)
                .ThenByDescending(c => c.Duration);

            foreach (var course in courses)
            {
                Console.WriteLine("--" + course.Name);
                Console.WriteLine("Start date: " + course.StartDate.ToShortDateString());
                Console.WriteLine("End date: " + course.EndDate.ToShortDateString());
                Console.WriteLine("Duration: " + course.Duration + " days");
                Console.WriteLine("Students enrolled: " + course.StudentsCount);

                Console.WriteLine();
            }
        }
Exemplo n.º 20
0
        public static void ListAllStudents()
        {
            using (var db = new StudentSystemContext())
            {
                var students = db.Students;

                foreach (var student in students)
                {
                    Console.WriteLine("{0} {1} - Birth Date: {2}; Phone Number: {3}; Reg Date: {4}", student.FirstName,
                    student.LastName, student.BirthDate, student.PhoneNumber, student.RegistrationDate);
                }
            }
        }
Exemplo n.º 21
0
        private static void task5(StudentSystemContext context)
        {
            var students = context.Students
                .OrderByDescending(s => s.Courses.Sum(c => c.Price))
                .ThenByDescending(s => s.Courses.Count)
                .ThenBy(s => s.Name)
                .Select(s => new
                {
                    Name = s.Name,
                    Courses = s.Courses.Count,
                    TotalPrice = s.Courses.Sum(c => c.Price),
                    AveragePrice = s.Courses.Average(c => c.Price)
                });

            foreach (var student in students)
            {
                Console.WriteLine($"{student.Name}, Courses count: {student.Courses}");
                Console.WriteLine($"Total Price: {student.TotalPrice:0.00}BGN , Average Price: {student.AveragePrice:0.00}BGN ");
                Console.WriteLine();
            }
        }
        public static void Main()
        {
            var context = new StudentSystemContext();

            // Problem 3

            // Task 1

            var studentsHomeworks = context.Students.Select(s => new
            {
                s.Name,
                Homeworks = s.Homeworks.Select(h => new
                {
                    h.Content,
                    h.ContentType
                }).ToList()
            });

            //foreach (var student in studentsHomeworks)
            //{
            //    Console.WriteLine("{0} - Homeworks: {1}", student.Name, student.Homeworks.Count());
            //}

            // Task 2

            var courseResources = context.Courses
                .OrderBy(c => c.StartDate)
                .ThenByDescending(c => c.EndDate)
                .Select(c => new
                {
                    c.Name,
                    c.Description,
                    c.Resources
                });

            // Task 3

            var courseOver5Resources = context.Courses
                .Where(c => c.Resources.Count > 5)
                .OrderByDescending(c => c.Resources.Count)
                .ThenBy(c => c.StartDate)
                .Select(c => new
                {
                    c.Name,
                    ResourceCount = c.Resources.Count
                });

            // Task 4

            var activeCourse = context.Courses
                .Where(c => c.StartDate < DateTime.Now && c.EndDate > DateTime.Now)
                .Select(c => new
                {
                    c.Name,
                    c.StartDate,
                    c.EndDate,
                    Duration = DbFunctions.DiffDays(c.EndDate, c.StartDate),
                    EnrolledStudents = c.Students.Count
                })
                .OrderByDescending(c => c.EnrolledStudents)
                .ThenByDescending(c => c.Duration);

            //foreach (var course in activeCourse)
            //{
            //    Console.WriteLine("{0} {1} {2} {3} {4}", course.Name, course.StartDate, course.EndDate, course.Duration, course.EnrolledStudents);
            //}

            // Task 5

            var studentsCourses = context.Students.Select(s => new
            {
                s.Name,
                NumberOfCOurses = s.Courses.Count,
                TotalPrice = s.Courses.Sum(c => c.Price),
                AveragePrice = s.Courses.Average(c => c.Price)
            })
            .OrderByDescending(s => s.TotalPrice)
            .ThenByDescending(s => s.NumberOfCOurses)
            .ThenBy(s => s.Name);
        }
        static void Main(string[] args)
        {
            var context = new StudentSystemContext();

            //1.Lists all students and their homework submissions. Select only their names and for each homework - content and content-type.

            //var studentsHomeworks = context.Students
            //    .Select(s => new
            //    {
            //        Name = s.Name,
            //        Homeworks = s.Courses.Select(c => c.Homeworks.Select(
            //            h => new
            //            {
            //                Content = "  ContentType: " + h.ContentType + ", content: " + h.Content
            //            })
            //            )
            //    });

            //foreach (var studentsHomework in studentsHomeworks)
            //{
            //    Console.WriteLine("Student " + studentsHomework.Name + "have submissions on: ");
            //    foreach (var homework in studentsHomework.Homeworks)
            //    {
            //        foreach (var hw in homework)
            //        {
            //            Console.WriteLine(hw.Content);
            //        }
            //    }
            //}

            //2.List all courses with their corresponding resources. Select the course name and description and everything for each resource. Order the courses by start date (ascending), then by end date (descending).

            //var courses = context.Courses
            //    .OrderBy(c => c.StartDate)
            //    .ThenByDescending(c => c.EndDate)
            //    .Select(c => new
            //{
            //    Name = c.Name,
            //    Description = c.Description,
            //    Resources = c.Resources.Select(r => new
            //    {
            //        Name = r.Name,
            //        Type = r.Type,
            //        Url = r.Url
            //    })
            //});

            //foreach (var course in courses)
            //{
            //    Console.WriteLine("{0}: {1} have as resource:", course.Name, course.Description);
            //    foreach (var resource in course.Resources)
            //    {
            //        Console.WriteLine("  {0} is of type {1} and the url is {2}", resource.Name, resource.Type, resource.Url);
            //    }
            //}

            //3.List all courses with more than 5 resources. Order them by resources count (descending), then by start date (descending). Select only the course name and the resource count.

            //var courses2 = context.Courses
            //    .OrderByDescending(c => c.Resources.Count)
            //    .ThenByDescending(c => c.StartDate)
            //    .Where(c => c.Resources.Count >= 5)
            //    .Select(c => new
            //{
            //    Name = c.Name,
            //    ResourceCount = c.Resources.Count
            //});

            //foreach (var course in courses2)
            //{
            //    Console.WriteLine("{0} have {1} resources", course.Name, course.ResourceCount);
            //}

            //4.List all courses which were active on a given date (choose the date depending on the data seeded to ensure there are results), and for each course count the number of students enrolled. Select the course name, start and end date, course duration (difference between end and start date) and number of students enrolled. Order the results by the number of students enrolled (in descending order), then by duration (descending).

            //var courses3 = context.Courses
            //    .OrderByDescending(c => c.Students.Count)
            //    .ThenByDescending(c => DbFunctions.DiffHours(c.StartDate, c.EndDate))
            //    .Where(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now)
            //    .Select(c => new
            //{
            //    Name = c.Name,
            //    StartDate = c.StartDate,
            //    EndDate = c.EndDate,
            //    Duration = DbFunctions.DiffHours(
            //         c.StartDate, c.EndDate),
            //    StudentsCount = c.Students.Count
            //});

            //foreach (var course in courses3)
            //{
            //    Console.WriteLine("{0} started {1} and ends {2}. The duration is {4} hours and the students enrolled are {3}",
            //        course.Name, course.StartDate, course.EndDate, course.StudentsCount, course.Duration);
            //}

            //5.For each student, calculate the number of courses she’s enrolled in, the total price of these courses and the average price per course for the student. Select the student name, number of courses, total price and average price. Order the results by total price (descending), then by number of courses (descending) and then by the student’s name (ascending).

            var students = context.Students
                .OrderByDescending(s => s.Courses.Sum(c => c.Price))
                .ThenByDescending(s => s.Courses.Count)
                .ThenBy(s => s.Name)
                .Select(s => new
                {
                    Name = s.Name,
                    CoursesCount = (int?)s.Courses.Count,
                    CoursesSum = (decimal?)s.Courses.Sum(c => c.Price),
                    CoursesAverage = (decimal?)s.Courses.Average(c => c.Price)
                });

            foreach (var student in students)
            {
                Console.WriteLine("{0} has enrolled in {1} courses. The total price for the courses is {2} and the average price per course is {3}", student.Name, student.CoursesCount, student.CoursesSum, student.CoursesAverage);
            }
        }
        private static void ListStudentsNumberOfCoursesAndPrice(StudentSystemContext ctx)
        {
            var students = ctx.Students
                .Select(s => new
                {
                    s.Name,
                    CoursesCount = s.Courses.Count,
                    TotalPrice = s.Courses.Sum(c => c.Price),
                    AveragePricePerCourse = s.Courses.Sum(c => c.Price) / s.Courses.Count
                })
                .OrderByDescending(s => s.TotalPrice)
                .ThenByDescending(s => s.CoursesCount)
                .ThenBy(s => s.Name);

            foreach (var student in students)
            {
                Console.WriteLine("--" + student.Name);
                Console.WriteLine("Courses count: " + student.CoursesCount);
                Console.WriteLine("Total price: " + student.TotalPrice);
                Console.WriteLine("Average price per course: " + student.AveragePricePerCourse);
            }
        }
Exemplo n.º 25
0
        public void ImportStudents(int numberOfStudents)
        {
            Console.Write("Importing Students");
            var db = new StudentSystemContext();

            for (int i = 0; i < numberOfStudents; i++)
            {
                var student = new Student()
                {
                    Name = RandomGenerator.GetString(2, 50),
                    Number = RandomGenerator.GetInt(7000000, 8000000)
                };

                db.Students.Add(student);

                if (i % 50 == 0)
                {
                    Console.Write(".");
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentSystemContext();
                }
            }

            db.SaveChanges();
            Console.WriteLine(" Completed!");
        }
Exemplo n.º 26
0
        private static void task4(StudentSystemContext context)
        {
            var date = new DateTime(2015, 04, 05);

            var courses = context.Courses
                .Where(c => DateTime.Compare(c.StartDate, date) <= 0 && DateTime.Compare(c.EndDate, date) >= 0)
                .OrderByDescending(c => c.Students.Count)
                .ThenByDescending(c => DbFunctions.DiffDays(c.StartDate, c.EndDate))
                .Select(c => new
                {
                    Name = c.Name,
                    StartDate = c.StartDate,
                    EndDate = c.EndDate,
                    CourseDuration = DbFunctions.DiffDays(c.StartDate, c.EndDate),
                    StudentsEnrolled = c.Students.Count
                });

            foreach (var course in courses)
            {
                Console.WriteLine(course.Name + " " + 
                    course.StartDate + " - " +
                    course.EndDate + " : " + 
                    course.CourseDuration + " Days");
                Console.WriteLine("Students: " + course.StudentsEnrolled);
                Console.WriteLine();
            }
        }
Exemplo n.º 27
0
        public void StudentCoursesConnect(int averageCoursesPerStudent)
        {
            Console.WriteLine("Create students - courses relations.");
            var db = new StudentSystemContext();
            var students = db.Students.Select(s => s).ToList();
            var courses = db.Courses.Select(c => c).ToList();
            var coursesCount = courses.Count();

            foreach (var student in students)
            {
                var numberOfCoursesForCurrentStudent = RandomGenerator.GetInt(averageCoursesPerStudent - 5, averageCoursesPerStudent + 5);

                for (int i = 0; i < numberOfCoursesForCurrentStudent; i++)
                {
                    student.Courses.Add(courses[RandomGenerator.GetInt(0, coursesCount - 1)]);
                }

                Console.Write(".");
            }

            db.SaveChanges();
            Console.WriteLine("Student Courses relation process completed.");
        }