public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            var address = new Address
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

            context.Add(address);

            context.Employees.First(e => e.LastName == "Nakov").Address = address;

            context.SaveChanges();

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

            var result = new StringBuilder();

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

            return(result.ToString().TrimEnd());
        }
Example #2
0
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            var newAddress = new Address
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

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

            nakov.Address = newAddress;

            context.Add(newAddress);
            context.Update(nakov);
            context.SaveChanges();

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

            var sb = new StringBuilder();

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

            var result = sb.ToString().Trim();

            return(result);
        }
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            StringBuilder sb      = new StringBuilder();
            var           address = new Address()
            {
                AddressText = "Vitoshka 15",
                TownId      = 4
            };

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

            employee.Address = address;

            context.SaveChanges();

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

            foreach (var item in employees)
            {
                sb.AppendLine($"{item.Address}");
            }
            return(sb.ToString().Trim());
        }
Example #4
0
        //Problem06
        public static string AddNewAddressToEmployee(SoftUniContext context)
        {
            Address newAddress = new Address()
            {
                AddressText = "Vitoshka 15", TownId = 4
            };

            context.Add(newAddress);
            context.SaveChanges();

            var targetEmployee = context
                                 .Employees
                                 .Where(x => x.LastName == "Nakov")
                                 .FirstOrDefault();

            targetEmployee.AddressId = newAddress.AddressId;
            context.SaveChanges();

            var allEmployeeAddresses = context
                                       .Employees
                                       .Join(context.Addresses,
                                             employees => employees.AddressId,
                                             address => address.AddressId,
                                             (employee, address) => new
            {
                AddressId   = employee.AddressId,
                AddressText = address.AddressText
            })
                                       .OrderByDescending(x => x.AddressId)
                                       .Take(10);

            StringBuilder sb = new StringBuilder();

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

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