public static string DeleteProjectById(SoftUniContext context)
        {
            var employeeProjects = context.EmployeesProjects
                                   .Where(x => x.ProjectId == 2)
                                   .ToList();

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

            foreach (var pr in employeeProjects)
            {
                context.Remove(pr);
            }

            context.Remove(project);

            context.SaveChanges();

            var projects = context.Projects
                           .Select(x => new
            {
                x.Name
            })
                           .Take(10)
                           .ToList();

            var sb = new StringBuilder();

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

            return(sb.ToString().TrimEnd());
        }
Пример #2
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var employeeProjectToDelete = context.EmployeesProjects.Where(e => e.ProjectId == 2).ToArray();

            foreach (var projectToDeleteFromTable in employeeProjectToDelete)
            {
                context.Remove(projectToDeleteFromTable);
            }

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

            context.Remove(projectToDelete);

            context.SaveChanges();

            var selectedProjects = context.Projects
                                   .Select(e => new
            {
                Name = e.Name
            })
                                   .Take(10);

            StringBuilder sb = new StringBuilder();

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

            return(sb.ToString().TrimEnd());
        }
Пример #3
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var project = context.Projects.FirstOrDefault(p => p.ProjectId == 2);

            var empProject = context.EmployeesProjects.Where(x => x.ProjectId == 2).ToArray();

            foreach (var proj in empProject)
            {
                context.Remove(proj);
            }

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

            var projects = context.Projects.Select(p => new
            {
                p.Name
            })
                           .Take(10)
                           .ToArray();

            StringBuilder sb = new StringBuilder();

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

            return(sb.ToString().Trim());
        }
Пример #4
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var employeeProjects = context.EmployeesProjects
                                   .Where(ep => ep.ProjectId == 2)
                                   .ToList();

            foreach (var item in employeeProjects)
            {
                context.Remove(item);
            }

            context.SaveChanges();

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

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

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

            var sb = new StringBuilder();

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

            return(sb.ToString().TrimEnd());
        }
Пример #5
0
        public static string RemoveTown(SoftUniContext context)
        {
            var addressesCountBeforeDelete = context.Addresses.Count();

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

            foreach (var address in addressesToDelete)
            {
                var employees = context.Employees
                                .Where(x => x.AddressId == address.AddressId)
                                .ToList();

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


                context.Remove(address);
            }

            context.SaveChanges();

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

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

            var addressesCountAfterDelete = context.Addresses.Count();

            return($"{addressesCountBeforeDelete - addressesCountAfterDelete} addresses in Seattle were deleted");
        }
        public static string RemoveTown(SoftUniContext context)
        {
            var seattle = context.Towns
                          .Where(x => x.Name == "Seattle")
                          .FirstOrDefault();

            var addressesInSeattle = context.Addresses
                                     .Where(x => x.TownId == seattle.TownId)
                                     .ToList();

            var addressesInSeattleList = addressesInSeattle.Select(x => x.AddressId).ToList();

            var employeesInSeattle = context.Employees
                                     .Where(x => x.AddressId != null && addressesInSeattleList.Contains(x.AddressId.Value));

            foreach (var employee in employeesInSeattle)
            {
                context.Remove(employee);
            }

            foreach (var address in addressesInSeattle)
            {
                context.Remove(address);
            }

            context.Remove(seattle);

            context.SaveChanges();

            return($"{addressesInSeattle.Count} addresses in Seattle were deleted");
        }
Пример #7
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            int projectId = 2;

            Project project = context.Projects.Find(projectId);

            int employeeId = context.EmployeesProjects
                             .Where(p => p.ProjectId == projectId)
                             .Select(e => e.EmployeeId).First();

            EmployeeProject[] employeeProject = context.EmployeesProjects.Where(ep => ep.ProjectId == projectId).ToArray();

            foreach (var ep in employeeProject)
            {
                context.Remove(ep);
            }

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

            StringBuilder sb = new StringBuilder();

            foreach (var p in context.Projects.Take(10).ToList())
            {
                sb.AppendLine(p.Name);
            }
            return(sb.ToString().TrimEnd());
        }
Пример #8
0
        public static string RemoveTown(SoftUniContext context)
        {
            const string townName = "Seattle";

            var employeeAddressesToSetToNull = context.Employees
                                               .Where(a => a.Address.Town.Name == townName)
                                               .ToArray();

            foreach (var employeeAddress in employeeAddressesToSetToNull)
            {
                employeeAddress.AddressId = null;
            }

            var addressesToDelete = context.Addresses
                                    .Where(e => e.Town.Name == townName)
                                    .ToArray();

            int countOfDeletedAddresses = 0;

            foreach (var address in addressesToDelete)
            {
                context.Remove(address);
                countOfDeletedAddresses++;
            }

            var townObject = context.Towns
                             .First(t => t.Name == townName);

            context.Remove(townObject);

            context.SaveChanges();

            return($"{countOfDeletedAddresses} addresses in Seattle were deleted");
        }
Пример #9
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var project = context.Projects.Find(2);

            context.EmployeesProjects
            .Where(x => x.ProjectId == project.ProjectId)
            .ToList()
            .ForEach(ep => context.EmployeesProjects.Remove(ep));

            context.Remove(project);

            context.SaveChanges();

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

            var result = new StringBuilder();

            foreach (var proj in projects)
            {
                result.AppendLine(proj);
            }

            return(result.ToString().TrimEnd());
        }
Пример #10
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");
        }
Пример #11
0
        //14.	Delete Project by Id
        public static string DeleteProjectById(SoftUniContext context)
        {
            var sb = new StringBuilder();

            const int ProjectToBeDeletedId = 2;

            var projectToBeDeleted = context
                                     .Projects
                                     .First(t => t.ProjectId == 2);

            var employeeProjectsToBeDeleted = context
                                              .EmployeesProjects
                                              .Where(ep => ep.Project.ProjectId == ProjectToBeDeletedId)
                                              .ToList();

            context.EmployeesProjects.RemoveRange(employeeProjectsToBeDeleted);
            context.SaveChanges();

            context.Remove(projectToBeDeleted);
            context.SaveChanges();

            foreach (string projectName in context.Projects.Select(p => p.Name).Take(10))
            {
                sb.AppendLine(projectName);
            }

            return(sb.ToString().TrimEnd());
        }
Пример #12
0
        public static string RemoveTown(SoftUniContext context)
        {
            int countDeletedAddresses = 0;

            var seattle          = context.Towns.FirstOrDefault(t => t.Name == "Seattle");
            var seattleAddresses = context.Addresses.Where(a => a.Town.Name == "Seattle").ToArray();
            int seattleId        = context.Towns.Where(t => t.Name == "Seattle").Select(i => i.TownId).Single();
            var employees        = context.Employees.ToArray();

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

            foreach (var address in seattleAddresses)
            {
                context.Addresses.Remove(address);
                countDeletedAddresses++;
            }

            context.Remove(seattle);
            context.SaveChanges();

            string result = $"{countDeletedAddresses} addresses in Seattle were deleted";

            return(result);
        }
Пример #13
0
        //15.	Remove Town
        public static string RemoveTown(SoftUniContext context)
        {
            var townToDelete = context
                               .Towns
                               .First(t => t.Name == "Seattle");

            IQueryable <Address> addressesToDelete =
                context
                .Addresses
                .Where(a => a.TownId == townToDelete.TownId);

            int addressesCount = addressesToDelete.Count();

            IQueryable <Employee> employeesOnDeletedAddresses =
                context
                .Employees
                .Where(e => addressesToDelete.Any(a => a.AddressId == e.AddressId));

            foreach (var employee in employeesOnDeletedAddresses)
            {
                employee.AddressId = null;
            }

            foreach (var address in addressesToDelete)
            {
                context.Addresses.Remove(address);
            }

            context.Remove(townToDelete);

            context.SaveChanges();

            return($"{addressesCount} addresses in Seattle were deleted");
        }
Пример #14
0
        //14.	Delete Project by Id
        public static string DeleteProjectById(SoftUniContext context)
        {
            StringBuilder sb = new StringBuilder();

            Project project = context.Projects.Find(2);

            var epCol = context.EmployeesProjects.Where(x => x.ProjectId == 2).ToList();

            foreach (var item in epCol)
            {
                context.Remove(item);
            }

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

            Project[] projects = context.Projects.Take(10).ToArray();

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

            return(sb.ToString().TrimEnd());
        }
Пример #15
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());
        }
Пример #16
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);
        }
Пример #17
0
        //Problem15 --X
        public static string RemoveTown(SoftUniContext context)
        {
            var empInSettle = context
                              .Employees
                              .Where(x => x.Address.Town.Name == "Seattle")
                              .ToList();

            foreach (var emp in empInSettle)
            {
                emp.AddressId = null;
            }

            var countOfAddesses = context
                                  .Addresses
                                  .Where(x => x.Town.Name == "Seattle")
                                  .Count();

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

            context.Addresses.RemoveRange(removeAddresses);
            context.SaveChanges();

            var city = context
                       .Towns
                       .Where(x => x.Name == "Seattle")
                       .FirstOrDefault();

            context.Remove(city);
            context.SaveChanges();

            return($"{countOfAddesses} addresses in Seattle were deleted");
        }
Пример #18
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            var projectDel = context.Projects
                             .FirstOrDefault(p => p.ProjectId == 2);

            var employeeProjDel = context.EmployeesProjects
                                  .Where(p => p.ProjectId == 2)
                                  .ToList();

            foreach (var emplProj in employeeProjDel)
            {
                context.EmployeesProjects.Remove(emplProj);
            }

            context.Remove(projectDel);

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

            var sb = new StringBuilder();

            foreach (var proj in projects)
            {
                sb.AppendLine($"{proj}");
            }

            return(sb.ToString().TrimEnd());
        }
Пример #19
0
        //Task 15
        public static string RemoveTown(SoftUniContext context)
        {
            const string wantedTownName = "Seattle";
            var          wantedTown     = context.Towns.First(x => x.Name == wantedTownName);
            int          wantedTownId   = wantedTown.TownId;

            context.Employees
            .Where(x => x.Address.TownId == wantedTownId)
            .ToList()
            .ForEach(x => x.Address = null);

            var addresses = context.Addresses
                            .Where(x => x.TownId == wantedTownId)
                            .ToList();
            int deletedAddressesCount = addresses.Count;

            addresses.ForEach(x => context.Remove(x));

            context.Towns
            .Remove(wantedTown);

            context.SaveChanges();

            return($"{deletedAddressesCount} addresses in Seattle were deleted");
        }
Пример #20
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());
        }
        //Problem 14

        public static string DeleteProjectById(SoftUniContext context)
        {
            var projectMapForDelete = context.EmployeesProjects
                                      .Where(ep => ep.ProjectId == 2)
                                      .ToList();

            foreach (var p in projectMapForDelete)
            {
                context.Remove(p);
            }

            var projectForDelete = context.Projects.Find(2);

            context.Projects.Remove(projectForDelete);

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


            var sb = new StringBuilder();

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

            return(sb.ToString().TrimEnd());
        }
        public static string DeleteProjectById(SoftUniContext context)
        {
            /*Let's delete the project with id 2.
             * Then, take 10 projects and return their names, each on a new line.
             * Remember to restore your database after this task.*/

            var epContext = context
                            .EmployeesProjects
                            .Where(ep => ep.ProjectId == 2) // put in variable
                            .ToList();

            foreach (var c in epContext)
            { // remove from employeesprojects first
                context.Remove(c);
            }

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

            foreach (var c in pContext)
            { // then from projects
                context.Remove(c);
            }

            context.SaveChanges();

            StringBuilder sb = new StringBuilder();

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

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

            return(sb.ToString().TrimEnd());
        }
Пример #23
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");
        }
        public static string RemoveTown(SoftUniContext context)
        {
            /*Write a program that deletes a town with name „Seattle”.
             * Also, delete all addresses that are in those towns.
             * Return the number of addresses that were deleted in format “{count} addresses in Seattle were deleted”.
             * There will be employees living at those addresses, which will be a problem when trying to delete the addresses.
             * So, start by setting the AddressId of each employee for the given address to null.
             * After all of them are set to null, you may safely remove all the addresses from the context.Addresses and finally remove the given town.*/

            var townToDelete = context
                               .Towns
                               .FirstOrDefault(t => t.Name == "Seattle"); // can make into variable

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

            var employeesOnAddressesList = context
                                           .Employees
                                           .Where(e => addressesList.Contains(e.Address))
                                           .ToList();

            foreach (var e in employeesOnAddressesList)
            { // make employees address null
                e.Address = null;
            }

            foreach (var a in addressesList)
            { // delete addresses that are in Seattle
                context.Remove(a);
            }

            // delete Seattle town
            context.Remove(townToDelete);

            context.SaveChanges();

            return($"{addressesList.Count} addresses in Seattle were deleted");
        }
        public static string RemoveTown(SoftUniContext context)
        {
            string townName = "Seattle";

            int townId = context.Towns
                         .Where(t => t.Name == townName)
                         .Select(t => t.TownId)
                         .FirstOrDefault();

            Town town = context.Towns
                        .Where(t => t.Name == townName)
                        .FirstOrDefault();

            var employees = context.Employees
                            .Where(e => e.Address.Town.Name == townName)
                            .ToList();

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

            var addresses = context.Addresses
                            .Where(a => a.TownId == townId)
                            .ToList();

            int countAddresses = addresses.Count;

            foreach (var address in addresses)
            {
                context.Remove(address);
                context.SaveChanges();
            }

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

            return($"{countAddresses} addresses in Seattle were deleted");
        }
Пример #26
0
        //15. Remove Town
        public static string RemoveTown(SoftUniContext context)
        {
            var townNameToDelete = "Seattle";

            var townToDelete = context.Towns
                               .Where(t => t.Name == townNameToDelete)
                               .FirstOrDefault();

            var targetingAddresses = context.Addresses
                                     .Where(a => a.Town.Name == townNameToDelete)
                                     .ToList();

            var employeesLivingOnTargetingAddresses = context.Employees
                                                      .Where(e => targetingAddresses.Contains(e.Address))
                                                      .ToList();

            employeesLivingOnTargetingAddresses.ForEach(e => e.Address = null);
            targetingAddresses.ForEach(a => context.Remove(a));
            context.Remove(townToDelete);

            context.SaveChanges();

            return($"{targetingAddresses.Count} addresses in Seattle were deleted");
        }
Пример #27
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);
        }
Пример #28
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)));
        }
Пример #29
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());
        }