public static void Main(string[] args)
        {
            using (var context = new SoftUniContext())
            {
                var projToDelete = context.Projects.Find(2);
                var empProjects  = context.EmployeesProjects.Where(ep => ep.ProjectId == 2);

                context.EmployeesProjects.RemoveRange(empProjects);
                context.Projects.Remove(projToDelete);

                context.SaveChanges();

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

                foreach (var proj in First10Projects)
                {
                    Console.WriteLine(proj.Name);
                }
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            using (var context = new SoftUniContext())
            {
                var townName = Console.ReadLine();

                var adressesInTown = context.Addresses
                                     .Where(a => a.Town.Name.Equals(townName)).ToArray();

                var deletedAddresses = 0;

                foreach (var address in adressesInTown)
                {
                    var employeesOnAddress = context.Employees
                                             .Where(e => e.Address.AddressId == address.AddressId);

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

                    context.Addresses.Remove(address);
                    deletedAddresses++;
                }

                var town = context.Towns.FirstOrDefault(t => t.Name.Equals(townName));

                context.Towns.Remove(town);

                context.SaveChanges();

                Console.WriteLine($"{deletedAddresses} address in {townName} was deleted");
            }
        }
Пример #3
0
        public static void Main()
        {
            using (var context = new SoftUniContext())
            {
                string townName = Console.ReadLine();

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

                int addressesCount = context.Addresses
                                     .Where(a => a.Town.Name == townName)
                                     .Count();

                context.Addresses
                .Where(a => a.Town.Name == townName)
                .ToList()
                .ForEach(a => context.Addresses.Remove(a));

                context.Towns
                .Remove(context.Towns
                        .SingleOrDefault(t => t.Name == townName));

                context.SaveChanges();

                Console.WriteLine($"{addressesCount} {(addressesCount == 1 ? "address" : "addresses")} in {townName} {(addressesCount == 1 ? "was" : "were")} deleted");
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            var departmentsSalariesToBeIncrease = new List <string>
            {
                "Engineering",
                "Tool Design",
                "Marketing",
                "Information Services"
            };

            using (SoftUniContext db = new SoftUniContext())
            {
                var employees = db.Employees
                                .Where(e => departmentsSalariesToBeIncrease.Contains(e.Department.Name))
                                .OrderBy(e => e.FirstName)
                                .ThenBy(e => e.LastName)
                                .ToList();
                employees.ForEach(e => e.Salary *= 1.12m);
                db.SaveChanges();

                using (StreamWriter Writer = new StreamWriter(@"..\..\..\result.txt"))
                {
                    foreach (var emp in employees)
                    {
                        Writer.WriteLine($"{emp.FirstName} {emp.LastName} (${emp.Salary:f2})");
                    }
                }
            }
        }
        private static void P14_DeleteProjectById(SoftUniContext db)
        {
            var projectId         = 2;
            var employeesProjects = db.EmployeesProjects
                                    .Where(e => e.ProjectId == projectId);

            foreach (var employeesProject in employeesProjects)
            {
                db.EmployeesProjects.Remove(employeesProject);
            }

            var projectToRemove = db.Projects
                                  .Find(projectId);

            db.Projects.Remove(projectToRemove);

            db.SaveChanges();

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

            foreach (var project in projects)
            {
                Console.WriteLine($"{project}");
            }
        }
        public static string RemoveTown(SoftUniContext context, string townName)
        {
            var townForDelete = context.Towns
                                .FirstOrDefault(t => t.Name == townName);

            var addressesInTown = context.Addresses
                                  .Where(a => a.Town == townForDelete);

            var employeesWithTheRelevantAddress = context.Employees
                                                  .Where(e => addressesInTown.Contains(e.Address));

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

            var removedAddresses = addressesInTown.Count();

            context.Addresses.RemoveRange(addressesInTown);
            context.Towns.Remove(townForDelete);
            context.SaveChanges();

            var result = $"{removedAddresses} addresses in {townName} were deleted";

            return(result
                   .TrimEnd());
        }
        public static string RemoveTown(SoftUniContext context)
        {
            StringBuilder sb       = new StringBuilder();
            string        townName = Console.ReadLine();

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

            int addressesCount = context.Addresses
                                 .Where(a => a.Town.Name == townName)
                                 .Count();

            context.Addresses
            .Where(a => a.Town.Name == townName)
            .ToList()
            .ForEach(a => context.Addresses.Remove(a));

            context.Towns
            .Remove(context.Towns
                    .SingleOrDefault(t => t.Name == townName));

            context.SaveChanges();


            Console.WriteLine($"{addressesCount} {(addressesCount == 1 ? "address" : "addresses")} in {townName} {(addressesCount == 1 ? "was" : "were")} deleted");
            return(sb.ToString().TrimEnd());
        }
        static void Main()
        {
            using (SoftUniContext context = new SoftUniContext())
            {
                string         townName  = Console.ReadLine();
                Town           town      = context.Towns.FirstOrDefault(t => t.Name == townName);
                List <Address> addresses = context.Addresses
                                           .Include(a => a.Employees)
                                           .Where(a => a.TownId == town.TownId)
                                           .ToList();

                addresses.ForEach(a =>
                {
                    a.Employees.ToList().ForEach(e => e.AddressId = null);
                    a.Employees.Clear();
                });

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

                Console.WriteLine("{0} address in {1} was deleted", addresses.Count(), townName);
                //ss
            }
        }
        static void Main(string[] args)
        {
            Address newAddress = new Address {
                AddressText = "Vitoshka 15", TownId = 4
            };

            using (SoftUniContext context = new SoftUniContext())
            {
                Employee employee = context.Employees.FirstOrDefault(x => x.LastName == "Nakov");
                employee.Address = newAddress;

                context.SaveChanges();

                var employees = context.Employees.OrderByDescending(x => x.Address.AddressId).Take(10).Select(x => new { AdressText = x.Address.AddressText });

                using (StreamWriter writer = new StreamWriter("../ExerciseOutput.txt"))
                {
                    foreach (var e in employees)
                    {
                        Console.WriteLine($"{e.AdressText}");
                        writer.WriteLine($"{e.AdressText}");
                    }
                }
            }
        }
Пример #10
0
        private static void RemoveTown(SoftUniContext db, string townName)
        {
            var townToDelete = db.Towns
                               .Where(x => x.Name == townName)
                               .FirstOrDefault();

            var adressesToDelete = db.Addresses
                                   .Where(x => x.Town == townToDelete);
            int count = 0;

            foreach (var employee in db.Employees)
            {
                foreach (var address in adressesToDelete)
                {
                    if (employee.AddressId == address.AddressId)
                    {
                        employee.AddressId = null;
                        count++;
                    }
                }
            }
            foreach (var address in adressesToDelete)
            {
                db.Addresses.Remove(address);
            }
            db.Towns.Remove(townToDelete);
            db.SaveChanges();

            Console.WriteLine($"{count} address in {townName} was deleted");
        }
Пример #11
0
        public static void Main()
        {
            using (var context = new SoftUniContext())
            {
                var employees = context.Employees
                                .Where(e => e.Department.Name == "Engineering" ||
                                       e.Department.Name == "Tool Design" ||
                                       e.Department.Name == "Marketing" ||
                                       e.Department.Name == "Information Services")
                                .OrderBy(e => e.FirstName)
                                .ThenBy(e => e.LastName)
                                .ToArray();

                using (var sr = new StreamWriter("..\\output.txt"))
                {
                    foreach (var employee in employees)
                    {
                        employee.Salary += employee.Salary * 0.12m;

                        sr.WriteLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F2})");
                    }
                }

                context.SaveChanges();
            }
        }
Пример #12
0
        private static string AddNewAddresAndUpdateEmployee(SoftUniContext db)
        {
            var address = new Address()
            {
                TownId = 4, AddressText = "Vitoshka 15"
            };

            db.Addresses.Add(address);
            var employee = db.Employees
                           .FirstOrDefault(e => e.LastName == "Nakov");

            employee.Address = address;

            db.SaveChanges();

            var result = db.Employees
                         .OrderByDescending(e => e.AddressId)
                         .Take(10)
                         .Select(e => e.Address.AddressText);

            var sb = new StringBuilder();

            foreach (var item in result)
            {
                sb.AppendLine(item);
            }
            return(sb.ToString().TrimEnd());
        }
Пример #13
0
        public static void Main()
        {
            const decimal IncreaseRate = 1.12m;

            var departmentsToIncreaseSalaries = new List <string>()
            {
                "Engineering",
                "Tool Design",
                "Marketing",
                "Information Services"
            };

            using (var db = new SoftUniContext())
            {
                var employees = db.Employees
                                .Where(e => departmentsToIncreaseSalaries.Contains(e.Department.Name))
                                .ToList();

                foreach (var employee in employees.OrderBy(e => e.FirstName).ThenBy(e => e.LastName))
                {
                    employee.Salary = employee.Salary * IncreaseRate;

                    db.SaveChanges();

                    Console.WriteLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F2})");
                }
            }
        }
Пример #14
0
        public static void Main(string[] args)
        {
            using (var context = new SoftUniContext())
            {
                var newAdress = new Address
                {
                    AddressText = "Vitoshka 15",
                    TownId      = 4,
                };

                var employee = context.Employees.FirstOrDefault(e => e.LastName.Equals("Nakov"));

                employee.Address = newAdress;

                context.SaveChanges();

                var employeesAddresses = context.Employees
                                         .OrderByDescending(e => e.AddressId)
                                         .Take(10)
                                         .Select(x => x.Address.AddressText)
                                         .ToList();

                using (StreamWriter sw = new StreamWriter("../Employees.txt"))
                {
                    employeesAddresses.ForEach(x => sw.WriteLine(x));
                }
            }
        }
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            var adress = new Address
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            var employee = context.Employees
                           .FirstOrDefault(e => e.LastName == "Nakov");

            employee.Address = adress;
            context.SaveChanges();

            var sb = new StringBuilder();

            var EmployeesAdresses = context.Employees
                                    .OrderByDescending(e => e.AddressId)
                                    .Select(e => new
            {
                e.Address.AddressText
            })
                                    .Take(10)
                                    .ToList();

            return(sb.Append(String.Join(Environment.NewLine, EmployeesAdresses
                                         .Select(ea => ea.AddressText)))
                   .ToString()
                   .TrimEnd());
        }
Пример #16
0
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            StringBuilder sb = new StringBuilder();

            var addres = new Address()
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            Employee nakov = context.Employees
                             .FirstOrDefault(x => x.LastName == "Nakov");

            nakov.Address = addres;

            context.SaveChanges();

            var employeeAdresses = context.Employees
                                   .OrderByDescending(x => x.AddressId)
                                   .Select(a => a.Address.AddressText)
                                   .Take(10)
                                   .ToList();

            foreach (var address in employeeAdresses)
            {
                sb.AppendLine(address);
            }

            return(sb.ToString().TrimEnd());
        }
        public static string DeleteProjectById(SoftUniContext context, int Id)
        {
            var sb = new StringBuilder();

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

            var employeesProjects = context.EmployeesProjects
                                    .Where(ep => ep.Project.ProjectId == project.ProjectId);

            context.EmployeesProjects.RemoveRange(employeesProjects);
            context.Projects.Remove(project);
            context.SaveChanges();

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

            foreach (var projectToPrint in projectsToPrint)
            {
                sb.AppendLine(projectToPrint.Name);
            }

            return(sb
                   .ToString()
                   .TrimEnd());
        }
Пример #18
0
        private static void Main(string[] args)
        {
            using (var db = new SoftUniContext())
            {
                var address = new Address();
                address.AddressText = "Vitoshka 15";
                address.TownId      = 4;

                Employee employee = db.Employees.FirstOrDefault(e => e.LastName == "Nakov");
                employee.Address = address;

                db.SaveChanges();

                var employees = db.Employees
                                .OrderByDescending(e => e.AddressId)
                                .Take(10)
                                .Select(e => new
                {
                    e.Address.AddressText
                })
                                .ToList();

                foreach (var a in employees)
                {
                    FileWriter.WriteLine(a.AddressText);
                }
            }
        }
Пример #19
0
        private void AddingNewAddressToEmployee(string addressText, int townId, string assignee)
        {
            using (var context = new SoftUniContext())
            {
                var address = context.Addresses.FirstOrDefault(a => a.AddressText == addressText);

                if (address == null)
                {
                    address = new Address()
                    {
                        AddressText = addressText,
                        TownId      = townId
                    };
                }

                foreach (var employee in context.Employees.Where(e => e.LastName == assignee))
                {
                    employee.Address = address;
                }

                context.SaveChanges();

                this.PrintFirstTenAddresses(context);
            }
        }
Пример #20
0
        public static void DeleteProjectById()
        {
            var dbContext = new SoftUniContext();

            using (dbContext)
            {
                var project = dbContext.EmployeesProjects
                              .Where(ep => ep.ProjectId == 2)
                              .ToList();
                dbContext.EmployeesProjects.RemoveRange(project);

                var projectFromProjects = dbContext.Projects
                                          .Where(p => p.ProjectId == 2)
                                          .ToList();
                dbContext.Projects.RemoveRange(projectFromProjects);

                dbContext.SaveChanges();

                var remainingProjects = dbContext.Projects
                                        .Take(10)
                                        .Select(p => p.Name)
                                        .ToList();

                foreach (var remainingProject in remainingProjects)
                {
                    Console.WriteLine(remainingProject);
                }
            }
        }
Пример #21
0
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            Address newAddress = new Address()
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            Employee Nakov = context.Employees.FirstOrDefault(x => x.LastName == "Nakov");

            Nakov.Address = newAddress;

            newAddress.Employees.Add(Nakov);

            context.SaveChanges();

            StringBuilder sb = new StringBuilder();

            var employees = context.Employees
                            .Select(x => new { x.Address.AddressText, x.AddressId })
                            .OrderByDescending(x => x.AddressId)
                            .Take(10);

            foreach (var employee in employees)
            {
                sb.AppendLine(employee.AddressText);
            }

            return(sb.ToString().TrimEnd());
        }
        static void Main(string[] args)
        {
            using (var context = new SoftUniContext())
            {
                var address = new Address
                {
                    AddressText = "Vitoshka 15",
                    TownId      = 4
                };

                Employee employee = context.Employees
                                    .FirstOrDefault(e => e.LastName == "Nakov");

                employee.Address = address;

                context.SaveChanges();

                string[] employeesAddresses = context.Employees
                                              .OrderByDescending(e => e.AddressId)
                                              .Take(10)
                                              .Select(e => e.Address.AddressText)
                                              .ToArray();

                using (var sw = new StreamWriter("../../../output.txt"))
                {
                    foreach (string empAddress in employeesAddresses)
                    {
                        sw.WriteLine(empAddress);
                    }
                }
            }
        }
        private static void P06_AddingANewAddressAndUpdatingEmployee(SoftUniContext db)
        {
            var address = new Address
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            db.Addresses.Add(address);

            var wantedEmployee = db.Employees
                                 .FirstOrDefault(e => e.LastName == "Nakov");

            if (wantedEmployee != null)
            {
                wantedEmployee.Address = address;
                db.SaveChanges();
            }

            var result = db.Employees
                         .OrderByDescending(e => e.AddressId)
                         .Take(10)
                         .Select(e => e.Address.AddressText);

            foreach (var res in result)
            {
                Console.WriteLine(res);
            }
        }
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            var address = new Address
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            var employee = context.Employees
                           .FirstOrDefault(e => e.LastName == "Nakov");

            address.Employees.Add(employee);

            employee.Address = address;

            context.SaveChanges();

            var employeesAddresses = context.Employees
                                     .OrderByDescending(e => e.AddressId)
                                     .Select(e => e.Address.AddressText)
                                     .Take(10)
                                     .ToList();

            var result = new StringBuilder();

            foreach (var addr in employeesAddresses)
            {
                result.AppendLine(addr);
            }

            return(result.ToString().TrimEnd());
        }
Пример #25
0
        public static void Main()
        {
            using (SoftUniContext context = new SoftUniContext())
            {
                try
                {
                    EmployeeProject[] referenceTableRecordsToDelete = context.EmployeesProjects.Where(id => id.ProjectId == 2).ToArray();
                    context.EmployeesProjects.RemoveRange(referenceTableRecordsToDelete);

                    Project toDelete = context.Projects.Single(id => id.ProjectId == 2);
                    context.Projects.Remove(toDelete);

                    context.SaveChanges();
                }
                catch (System.Exception e)
                {
                    System.Console.WriteLine(e.Message);
                }

                StringBuilder sb = new StringBuilder();

                context.Projects
                .Select(n => new
                {
                    n.Name
                })
                .Take(10)
                .ToList()
                .ForEach(r => sb.AppendLine($"{r.Name}"));

                FileCreator.CreateFile(sb.ToString());
            }
        }
Пример #26
0
        static void Main(string[] args)
        {
            using (SoftUniContext context = new SoftUniContext())
            {
                var project = context.Projects.FirstOrDefault(p => p.ProjectId == 2);

                var employeesProjects = context.EmployeesProjects.Where(x => x.ProjectId == 2);
                context.EmployeesProjects.RemoveRange(employeesProjects);

                context.Projects.Remove(project);

                context.SaveChanges();

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

                using (StreamWriter writer = new StreamWriter("../ExerciseOutput.txt"))
                {
                    foreach (var p in projects)
                    {
                        Console.WriteLine($"{p.Name}");
                        writer.WriteLine($"{p.Name}");
                    }
                }
            }
        }
Пример #27
0
        static void Main(string[] args)
        {
            using (SoftUniContext context = new SoftUniContext())
            {
                var projects = context.EmployeesProjects
                               .Where(x => x.ProjectId == 2);

                context.EmployeesProjects.RemoveRange(projects);

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

                context.Projects.Remove(project);

                context.SaveChanges();

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

                using (StreamWriter sw = new StreamWriter("../Employees.txt"))
                {
                    foreach (var p in proj)
                    {
                        sw.WriteLine(p.Name);
                    }
                }
            }
        }
Пример #28
0
        private static string AddNewAddressToEmployee(SoftUniContext context)
        {
            Address newAddress = new Address()
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            context.Addresses.Add(newAddress);

            Employee employeeNakov = context.Employees
                                     .Where(e => e.LastName == "Nakov")
                                     .FirstOrDefault();

            employeeNakov.Address = newAddress;
            context.SaveChanges();

            string[] employeesAddresses = context.Employees
                                          .OrderByDescending(e => e.AddressId)
                                          .Select(e => e.Address.AddressText)
                                          .Take(10)
                                          .ToArray();

            return(string.Join(Environment.NewLine, employeesAddresses));
        }
Пример #29
0
        public static string IncreaseSalaries(SoftUniContext context)
        {
            var departments = new string[] { "Engineering", "Tool Design", "Marketing", "Information Services" };

            var employees = context.Employees
                            .Where(x => departments.Contains(x.Department.Name))
                            .OrderBy(x => x.FirstName)
                            .ThenBy(x => x.LastName)
                            .ToList();

            foreach (var employee in employees)
            {
                employee.Salary *= 1.12m;
            }

            context.SaveChanges();

            var sb = new StringBuilder();

            foreach (var employee in employees)
            {
                sb.AppendLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F2})");
            }

            return(sb.ToString().TrimEnd());
        }
Пример #30
0
        static void Main(string[] args)
        {
            var context = new SoftUniContext();

            using (context)
            {
                var address = new Address()
                {
                    AddressText = "Vitoshka 15",
                    TownId      = 4,
                };

                Employee employee = context.Employees
                                    .FirstOrDefault(e => e.LastName == "Nakov");

                employee.Address = address;

                context.SaveChanges();

                var addressTexts = context.Employees
                                   .OrderByDescending(e => e.Address.AddressId)
                                   .Take(10)
                                   .Select(e => e.Address.AddressText)
                                   .ToArray();

                foreach (var addressText in addressTexts)
                {
                    Console.WriteLine(addressText);
                }
            }
        }