private static void ListAllCoursesWhichWereActiveOnGivenDate(StudentsContext context, DateTime targetDate) { var courses = context.Courses .Where(c => targetDate <= c.StartDate && targetDate <= c.EndDate) .Select(c => new { Name = c.Name, StartDate = c.StartDate, EndDate = c.EndDate, CourseDuration = SqlFunctions.DateDiff("day", c.StartDate, c.EndDate), NumberOfStudentsEnrolled = c.Students.Count }) .OrderByDescending(c => c.NumberOfStudentsEnrolled) .ThenByDescending(c => c.CourseDuration) .ToList(); StringBuilder content = new StringBuilder(); foreach (var course in courses) { content.AppendFormat("{0} {1} {2} {3} - {4}{5}", course.Name, course.StartDate, course.EndDate, course.CourseDuration, course.NumberOfStudentsEnrolled, Environment.NewLine); } Console.Write(content); }
private static void AllCoursesActiveOnGivenDate(StudentsContext context) { DateTime date = new DateTime(2017, 02, 16); var courses = context.Courses .Where(course => course.StartDate <= date && course.EndDate >= date) .OrderBy(course => course.Students.Count) .Select(course => new { course.Name, course.StartDate, course.EndDate, Duration = SqlFunctions.DateDiff("day", course.StartDate, course.EndDate), course.Students }); foreach (var course in courses.OrderByDescending(course => course.Duration)) { Console.WriteLine($"Course: {course.Name}"); Console.WriteLine($"Starting on: {course.StartDate.ToShortDateString()}"); Console.WriteLine($"Ending on: {course.EndDate.ToShortDateString()}"); Console.WriteLine($"Duration: {course.Duration} days"); Console.WriteLine($"Students in Course: {course.Students.Count}"); Console.WriteLine(); } }
static void Main(string[] args) { var context = new StudentsContext(); // /*01*/ ListAllStudentsAndTheirHomeworks(context); // /*02*/ ListAllCoursesWithResources(context); // /*03*/ ListAllCoursesWithMoreThan5Resources(context); // /*04*/ ListAllCoursesWhichWereActiveOnGivenDate(context, new DateTime(2017, 4, 20)); // /*05*/ CalculateTheNumberOfCoursesForEachStudent(context); }
private static void ListAllStudentsAndHomeworkSubmission(StudentsContext context) { var students = context.Students.Select(student => new { student.Name, student.Homeworks }); foreach (var student in students) { Console.WriteLine($"Student: {student.Name}"); Console.WriteLine("Student homeworks: "); foreach (var homework in student.Homeworks) { Console.WriteLine($"--{homework.Content} of Type: {homework.ContentType}"); } Console.WriteLine(); } }
private static void AllCoursesOfAllStudent(StudentsContext context) { var students = context.Students .OrderByDescending(student => student.Courses.Sum(c => c.Price)) .ThenByDescending(student => student.Courses.Count) .ThenBy(student => student.Name) .Select(student => new { student.Name, student.Courses }); foreach (var student in students) { Console.WriteLine($"Student: {student.Name}"); Console.WriteLine($"Number of Courses: {student.Courses.Count}"); Console.WriteLine($"Total price of Courses: {student.Courses.Sum(course => course.Price)}"); Console.WriteLine($"Average price of Courses: {student.Courses.Average(course => course.Price)}"); Console.WriteLine(); } }
private static void AllCoursesWithMoreThanFiveResources(StudentsContext context) { var courses = context.Courses .Where(course => course.Resources.Count > 5) .OrderByDescending(course => course.Resources.Count) .ThenByDescending(course => course.StartDate); foreach (var course in courses) { Console.WriteLine($"Course: {course.Name}"); Console.WriteLine($"Number of Resources: {course.Resources.Count}"); Console.WriteLine(); } if (!courses.Any()) { Console.WriteLine("There is no course with more than five resources!"); } }
private static void ListAllCoursesWithMoreThan5Resources(StudentsContext context) { var courses = context.Courses .Where(c => c.Resources.Count > 5) .OrderByDescending(c => c.Resources.Count) .ThenByDescending(c => c.StartDate) .Select(c => new { Name = c.Name, ResourcesCount = c.Resources.Count }); StringBuilder content = new StringBuilder(); foreach (var course in courses) { content.AppendLine($"{course.Name} - {course.ResourcesCount}"); } Console.Write(content); }
static void Main(string[] args) { StudentsContext ctx = new StudentsContext(); Student pesho = new Student() { Name = "Pesho", RegisteredOn = new DateTime(2016, 06, 02) }; Student tosho = new Student() { Name = "Todor", PhoneNumber = "+359899123932", BirthDate = new DateTime(1987, 01, 23), RegisteredOn = new DateTime(2016, 05, 15) }; Student stami = new Student() { Name = "Stamat", PhoneNumber = "+35985522341", RegisteredOn = new DateTime(2016, 05, 31) }; Course programingBasics = new Course() { Name = "Programing Basics June-August 2016", Description = "Entry module with one course", StartDate = new DateTime(2016, 06, 18), EndDate = new DateTime(2016, 08, 28) }; Resource videoLessonOne = new Resource("video") { Name = "Simple calculation.", Url = "https://softuni.bg/trainings/resources/video/9174/video-screen-18-06-2016-georgi-inkov-programming-basics-june-2016" }; pesho.Courses.Add(programingBasics); tosho.Courses.Add(programingBasics); stami.Courses.Add(programingBasics); programingBasics.Resourses.Add(videoLessonOne); ctx.SaveChanges(); }
private static void ListAllCoursesWithTheirResources(StudentsContext context) { var courses = context.Courses .OrderBy(course => course.StartDate) .ThenByDescending(course => course.EndDate) .Select(course => new { course.Name, course.Description, course.Resources }); foreach (var course in courses) { Console.WriteLine($"Course: {course.Name}"); Console.WriteLine($"Description: {course.Description}"); foreach (var resource in course.Resources) { Console.WriteLine($"--Resource Name: {resource.Name}"); Console.WriteLine($"--Resource Type: {resource.Type}"); Console.WriteLine($"--Resource URL: {resource.URL}"); } Console.WriteLine(); } }
private static void CalculateTheNumberOfCoursesForEachStudent(StudentsContext context) { var students = context.Students .Select(s => new { Name = 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); StringBuilder content = new StringBuilder(); foreach (var s in students) { content.AppendLine($"{s.Name} {s.NumberOfCourses} {s.TotalPrice} {s.AveragePrice}"); } Console.Write(content); }
private static void Main(string[] args) { // Task 1 - The database and the models are created; // Task 2 - The inserted sample data is in the Seed method in Migrations.Configuration; // Task 4 - Added migration 'AddedLicenseModel' for the creation of the License model and foreign key with the Resource model; StudentsContext context = new StudentsContext(); //Task 3.1: //ListAllStudentsAndHomeworkSubmission(context); //Task 3.2: //ListAllCoursesWithTheirResources(context); //Task 3.3: //AllCoursesWithMoreThanFiveResources(context); //Task 3.4: //AllCoursesActiveOnGivenDate(context); //Task 3.5: //AllCoursesOfAllStudent(context); }
private static void ListAllCoursesWithResources(StudentsContext context) { var courses = context.Courses .OrderBy(c => c.StartDate) .ThenByDescending(c => c.EndDate) .Select(c => new { Name = c.Name, Description = c.Description, Resources = c.Resources }); StringBuilder content = new StringBuilder(); foreach (var course in courses) { content.AppendLine($"{course.Name} {course.Description}"); foreach (var resource in course.Resources) { content.AppendLine($"--- {resource.Name} {resource.ResourceType} {resource.Url}"); } } Console.Write(content); }
private static void ListAllStudentsAndTheirHomeworks(StudentsContext context) { var students = context.Students .Select(s => new { Name = s.Name, Homeworks = s.Homeworks.Select(h => new { Content = h.Content, Type = h.ContentType }) }); StringBuilder content = new StringBuilder(); foreach (var student in students) { content.Append(student.Name); foreach (var homework in student.Homeworks) { content.Append($" - {homework.Content} - {homework.Type}"); } content.AppendLine(); } Console.WriteLine(content); }