Пример #1
0
        // 14. Delete Project by Id
        public static string DeleteProjectById(SoftUniContext context)
        {
            var sb = new StringBuilder();

            var employeesProject = context.EmployeesProjects
                                   .Where(ep => ep.ProjectId == 2);

            context.RemoveRange(employeesProject);

            var project = context.Projects
                          .FirstOrDefault(p => p.ProjectId == 2);

            context.Remove(project);
            context.SaveChanges();

            var projects = context.Projects
                           .Take(10)
                           .ToArray();

            foreach (var p in projects)
            {
                sb.AppendLine(p.Name);
            }

            return(sb.ToString().TrimEnd());
        }
Пример #2
0
        //15. Remove Town
        public static string RemoveTown(SoftUniContext context)
        {
            var sb = new StringBuilder();

            var town = context.Towns
                       .FirstOrDefault(t => t.Name == "Seattle");

            var addresses      = town.Addresses.ToList();
            var addressesCount = addresses.Count();

            foreach (var address in addresses)
            {
                foreach (var employee in address.Employees)
                {
                    employee.AddressId = null;
                }
            }

            context.RemoveRange(addresses);
            context.Remove(town);
            context.SaveChanges();

            sb.AppendLine($"{addressesCount} addresses in Seattle were deleted");

            return(sb.ToString().TrimEnd());
        }
Пример #3
0
        public static string RemoveTown(SoftUniContext context)
        {
            var addresses = context.Addresses
                            .Where(x => x.Town.Name == "Seattle")
                            .ToList();

            var employees = context.Employees
                            .Where(x => x.Address.Town.Name == "Seattle")
                            .ToList();

            foreach (var employee in employees)
            {
                employee.Address = null;
            }

            int count = addresses.Count();

            context.RemoveRange(addresses);

            var town = context.Towns
                       .Where(x => x.Name == "Seattle")
                       .ToList();

            context.Remove(town.First());
            context.SaveChanges();

            return($"{count} addresses in Seattle were deleted");
        }
Пример #4
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var result = new StringBuilder();

            var employeesProjects = context.EmployeesProjects
                                    .Where(ep => ep.ProjectId == 2)
                                    .ToList();

            context.RemoveRange(employeesProjects);

            var project = context.Projects
                          .Where(p => p.ProjectId == 2).FirstOrDefault();

            context.Remove(project);

            context.SaveChanges();

            var projects = context.Projects
                           .Select(p => p.Name)
                           .Take(10)
                           .ToList();

            projects.ForEach(p => result.AppendLine(p));

            return(result.ToString().Trim());
        }
Пример #5
0
        public static string RemoveTown(SoftUniContext context)
        {
            var employees = context.Employees
                            .Where(e => e.Address.Town.Name == "Seattle").ToList();

            employees.ForEach(e => e.AddressId = null);

            var numOfAddresses = context.Addresses
                                 .Where(a => a.Town.Name == "Seattle").Count();

            var listOfAddresses = context.Addresses
                                  .Where(a => a.Town.Name == "Seattle")
                                  .ToList();

            context.RemoveRange(listOfAddresses);

            var town = context.Towns
                       .Where(t => t.Name == "Seattle").FirstOrDefault();

            context.Remove(town);


            context.SaveChanges();

            var result = $"{numOfAddresses} addresses in Seattle were deleted";

            return(result);
        }
Пример #6
0
        public static string RemoveTown(SoftUniContext context)
        {
            var targetTown = context.Towns
                             .Where(x => x.Name == "Seattle")
                             .FirstOrDefault();

            var addresses = context.Addresses
                            .Where(x => x.Town.Name == "Seattle")
                            .ToList();

            var employees = context.Employees.
                            Where(x => x.Address.Town.Name == "Seattle")
                            .ToArray();



            foreach (var item in employees)
            {
                item.AddressId = null;
                context.SaveChanges();
            }


            var projects = context.EmployeesProjects
                           .Where(x => x.Employee.Address.Town.Name == "Seattle")
                           .ToList();

            if (projects != null)
            {
                context.EmployeesProjects.RemoveRange(projects);
                context.SaveChanges();
            }


            if (employees != null)
            {
                context.Employees.RemoveRange(employees);
                context.SaveChanges();
            }

            if (addresses != null)
            {
                context.RemoveRange(addresses);
                context.SaveChanges();
            }

            if (targetTown != null)
            {
                context.Remove(targetTown);
                context.SaveChanges();
            }

            return($"{addresses.Count} addresses in Seattle were deleted");
        }
Пример #7
0
        public static string RemoveTown(SoftUniContext context)
        {
            var seattleTown = context.Towns.First(x => x.Name == "Seattle");
            var addresses   = context.Addresses.Where(x => x.Town.TownId == seattleTown.TownId).ToList();

            var result = $"{addresses.Count()} addresses in Seattle were deleted";

            var addressesIds     = addresses.Select(x => x.AddressId).ToList();
            var seattleEmployees = context.Employees.Where(x => addressesIds.Contains(x.AddressId.Value)).ToList();

            seattleEmployees.ForEach(e => e.AddressId = null);

            context.UpdateRange(seattleEmployees);
            context.RemoveRange(addresses);
            context.Remove(seattleTown);

            return(result);
        }
Пример #8
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            const int projectToDelete  = 2;
            var       employeeProjects = context.EmployeesProjects.Where(ep => ep.ProjectId == projectToDelete).ToList();

            if (employeeProjects.Count > 0)
            {
                context.RemoveRange(employeeProjects);
            }

            var project = context.Projects.Find(projectToDelete);

            if (project != null)
            {
                context.Remove(project);
            }

            context.SaveChanges();

            return(string.Join(Environment.NewLine, context.Projects.Select(p => p.Name).Take(10)));
        }
Пример #9
0
        public static string RemoveTown(SoftUniContext context)
        {
            StringBuilder sb             = new StringBuilder();
            int           addresessCount = 0;

            context.Employees.Where(e => e.Address.Town.Name == "Seattle")
            .ToList()
            .ForEach(e => e.AddressId = null);

            var addresess = context.Addresses.Where(a => a.Town.Name == "Seattle").ToList();

            var town = context.Towns.FirstOrDefault(t => t.Name == "Seattle");

            addresessCount = addresess.Count();

            context.RemoveRange(addresess);

            context.Towns.Remove(town);

            context.SaveChanges();

            return($"{addresessCount} addresses in Seattle were deleted");
        }
Пример #10
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var deleteProject     = context.Projects.First(x => x.ProjectId == 2);
            var project2Employees = context.EmployeesProjects.Where(x => x.ProjectId == 2).ToList();
            var emplIds           = project2Employees.Select(x => x.EmployeeId).ToList();
            var employees         = context.Employees.Where(x => emplIds.Contains(x.EmployeeId)).ToList();

            employees.ForEach(e => e.EmployeesProjects = null);
            context.UpdateRange(employees);
            context.RemoveRange(project2Employees);
            context.Remove(deleteProject);
            context.SaveChanges();

            var projects = context.Projects.Take(10).ToList();

            var sb = new StringBuilder();

            foreach (var project in projects)
            {
                sb.AppendLine(project.Name);
            }

            return(sb.ToString().Trim());
        }