static void Main() { SoftUniDBContext context = new SoftUniDBContext(); string townName = Console.ReadLine(); var addressess = context.Towns.Where(t => t.Name == townName); var employees = context.Employees; foreach (var employee in employees) { foreach (var town in addressess) { if (employee.AddressID != null && town.Addresses.Contains(employee.Address)) { employee.AddressID = null; break; } } } foreach (var town in addressess) { Console.WriteLine($"{town.Addresses.Count} addresses in {town.Name} deleted"); town.Addresses.Clear(); context.Towns.Remove(town); } context.SaveChanges(); }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var project = context.Projects.FirstOrDefault(p => p.ProjectID == 2); //hardcoded - switch up to userinput // using SELECT before Contains because joining tables from Database to tables in memory is illegal var employeesOnProject = context.Employees.Where(e => e.Projects.Select(p => p.ProjectID).Contains(project.ProjectID)).ToList(); foreach (var employee in employeesOnProject) { employee.Projects.Remove(project); } context.Projects.Remove(project); context.SaveChanges(); var projects = context.Projects.Select(p => p.Name).Take(10).ToList(); projects.ForEach(x => Console.WriteLine(x)); using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); projects.ForEach(x => Console.WriteLine(x)); } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var deps = context.Departments .Where(d => d.Employees.Count > 5) .OrderBy(d => d.Employees.Count); //.Select(d => new {d.Name, d.Manager.FirstName, d.Employees}); foreach (var dep in deps) { Console.WriteLine($"{dep.Name} {dep.Manager.FirstName}"); var emps = dep.Employees.Select(e => new { e.FirstName, e.LastName, e.JobTitle }); foreach (var emp in emps) { Console.WriteLine($"{emp.FirstName} {emp.LastName} {emp.JobTitle}"); } } using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); foreach (var dep in deps) { Console.WriteLine($"{dep.Name} {dep.Manager.FirstName}"); var emps = dep.Employees.Select(e => new { e.FirstName, e.LastName, e.JobTitle }); foreach (var emp in emps) { Console.WriteLine($"{emp.FirstName} {emp.LastName} {emp.JobTitle}"); } } } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); string[] departments = new[] { "Engineering", "Tool Design", "Marketing", "Information Services" }; var employees = context.Employees.Where(e => departments.Contains(e.Department.Name)); foreach (var employee in employees) { employee.Salary *= 1.12m; Console.WriteLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F6})"); } context.SaveChanges(); using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); foreach (var employee in employees) { Console.WriteLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F6})"); } } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); Address adress = new Address(); adress.AddressText = "Vitoshka 15"; adress.TownID = 4; Employee nakov = context.Employees.FirstOrDefault(e => e.LastName == "Nakov"); nakov.Address = adress; context.SaveChanges(); var employeeAddressessTop10 = context.Employees.OrderByDescending(e => e.Address.AddressID).Take(10).Select(e => e.Address.AddressText).ToList(); employeeAddressessTop10.ForEach(x => Console.WriteLine(x)); using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); employeeAddressessTop10.ForEach(x => Console.WriteLine(x)); } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var employees = context.Employees.Where(e => e.Salary > 50000).Select(e => e.FirstName).ToList(); using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); employees.ForEach(x => Console.WriteLine(x)); } }
static void Main() { SoftUniDBContext dbContext = new SoftUniDBContext(); var employees = dbContext.Employees.Select(e => new { e.FirstName, e.LastName, e.MiddleName, e.JobTitle, e.Salary }) .ToList(); using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); employees.ForEach(x => Console.WriteLine($"{x.FirstName} {x.LastName} {x.MiddleName} {x.JobTitle} {x.Salary}")); } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var seattlers = context.Employees .Where(e => e.Department.Name == "Research and Development") .OrderBy(e => e.Salary) .ThenByDescending(e => e.FirstName) .Select(e => new { e.FirstName, e.LastName, e.Department.Name, e.Salary }) .ToList(); seattlers.ForEach(x => Console.WriteLine($"{x.FirstName} {x.LastName} from {x.Name} - {x.Salary:F2}")); // return; using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); seattlers.ForEach(x => Console.WriteLine($"{x.FirstName} {x.LastName} from {x.Name} - ${x.Salary:F2}")); } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); DateTime startDate = DateTime.Parse("2001.01.01"); DateTime endDate = DateTime.Parse("2003.01.01"); var thirtyEmployees = context.Employees .Where(e => e.Projects.Any(p => p.StartDate >= startDate && p.StartDate <= endDate)) .Take(30) .Select(e => new { e.FirstName, e.LastName, managerName = e.Manager.FirstName, e.Projects }); foreach (var employee in thirtyEmployees) { Console.Write($"{employee.FirstName} {employee.LastName} {employee.managerName} "); foreach (var project in employee.Projects) { Console.Write($"--{project.Name} {project.StartDate.ToString("G", CultureInfo.DefaultThreadCurrentCulture)} "); if (project.EndDate.HasValue) { Console.WriteLine(project.EndDate.Value.ToString("G", CultureInfo.DefaultThreadCurrentCulture)); } } } using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); foreach (var employee in thirtyEmployees) { Console.WriteLine($"{employee.FirstName} {employee.LastName} {employee.managerName}"); foreach (var project in employee.Projects) { Console.Write($"--{project.Name} {project.StartDate.ToString("G", CultureInfo.CreateSpecificCulture("en-us"))} "); if (project.EndDate.HasValue) { Console.WriteLine(project.EndDate.Value.ToString("G", CultureInfo.CreateSpecificCulture("en-us"))); } } } } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var projects = context.Projects.OrderByDescending(p => p.StartDate).Take(10); foreach (var project in projects.OrderBy(p => p.Name)) { Console.WriteLine($"{project.Name} {project.Description} {project.StartDate} {project.EndDate}"); } using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); foreach (var project in projects.OrderBy(p => p.Name)) { Console.WriteLine($"{project.Name} {project.Description} {project.StartDate} {project.EndDate}"); } } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var employee147 = context.Employees.FirstOrDefault(e => e.EmployeeID == 147); Console.WriteLine($@"{employee147.FirstName} {employee147.LastName} {employee147.JobTitle}"); foreach (var project in employee147.Projects.OrderBy(p => p.Name)) { Console.WriteLine($"{project.Name}"); } using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); Console.WriteLine($@"{employee147.FirstName} {employee147.LastName} {employee147.JobTitle}"); foreach (var project in employee147.Projects.OrderBy(p => p.Name)) { Console.WriteLine($"{project.Name}"); } } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var SAemployess = context.Employees.Where(e => e.FirstName.StartsWith("SA")) .Select(e => new { e.FirstName, e.LastName, e.JobTitle, e.Salary }); foreach (var employee in SAemployess) { Console.WriteLine($"{employee.FirstName} {employee.LastName} - {employee.JobTitle} - (${employee.Salary})"); } using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); foreach (var employee in SAemployess) { Console.WriteLine($"{employee.FirstName} {employee.LastName} - {employee.JobTitle} - (${employee.Salary})"); } } }
static void Main() { SoftUniDBContext context = new SoftUniDBContext(); var addressess = context.Addresses .OrderByDescending(a => a.Employees.Count) .ThenBy(a => a.Town.Name) //.Select(a => new {a.AddressText, a.Town.Name, a.Employees.Count, a.Employees}) <--- outputs wrong ordering, but WHY? .Take(10); foreach (var address in addressess) { Console.WriteLine($"{address.AddressText}, {address.Town.Name} - {address.Employees.Count} employees"); } using (StreamWriter writer = new StreamWriter(@"D:\output.txt")) { Console.SetOut(writer); foreach (var address in addressess) { Console.WriteLine($"{address.AddressText}, {address.Town.Name} - {address.Employees.Count} employees"); } } }