private static void Main(string[] args) { using(var context = new TelerikAcademyEntities()) { NonOptimizedEmployeeQuery(context); OptimizedEmployeeQuery(context); } }
private static void Main() { using (var context = new TelerikAcademyEntities()) { StringBuilder container = new StringBuilder(); AnonymusVariable(context, container); SeparateCalls(context, container); IncludeUsing(context, container); } }
private static void OptimizedEmployeeQuery(TelerikAcademyEntities context) { Console.WriteLine("Employees:"); var employeesInSofia = context.Employees .Where(e => e.Address.Town.Name == "Sofia") .Select(e => e.FirstName + " " + e.MiddleName + " " + e.LastName); foreach (var employee in employeesInSofia) { Console.WriteLine("{0}", string.Join(", ", employee)); } }
private static void NonOptimizedEmployeeQuery(TelerikAcademyEntities context) { var employees = context.Employees.ToList(); var addresses = employees.Select(e => e.Address).ToList(); var towns = addresses.Select(a => a.Town).ToList(); var sofiaTownId = towns.Where(t => t.Name.ToLowerInvariant() == "Sofia".ToLowerInvariant()).First().TownID; Console.WriteLine("Employees:"); foreach (var employee in employees.Where(e => e.Address.TownID == sofiaTownId).Select(e => e.FirstName + " " + e.MiddleName + " " + e.LastName)) { Console.WriteLine("{0}", string.Join(", ",employee)); } }
private static void IncludeUsing(TelerikAcademyEntities context, StringBuilder container) { Console.WriteLine("Include:"); var employeesWithTown = context.Employees.Include("Department").Include("Address.Town"); foreach (var includedEmployee in employeesWithTown) { container.Append("Name: ").Append(includedEmployee.FirstName).Append(' ').Append(includedEmployee.MiddleName).Append(' ').AppendLine(includedEmployee.LastName); container.Append("Department: ").AppendLine(includedEmployee.Department.Name); container.Append("Town: ").AppendLine(includedEmployee.Address.Town.Name); container.AppendLine(new string('-', 50)); } Console.WriteLine(container); container.Clear(); }
private static void SeparateCalls(TelerikAcademyEntities context, StringBuilder container) { Console.WriteLine("Separate: "); foreach (var separateEmployee in context.Employees) { container.Append("Name: ").Append(separateEmployee.FirstName).Append(' ').Append(separateEmployee.MiddleName).Append(' ').AppendLine(separateEmployee.LastName); container.Append("Department: ").AppendLine(separateEmployee.Department.Name); container.Append("Town: ").AppendLine(separateEmployee.Address.Town.Name); container.AppendLine(new string('-', 50)); } Console.WriteLine(container); container.Clear(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
private static void AnonymusVariable(TelerikAcademyEntities context, StringBuilder container) { var extractedEmployees = context.Employees.Select(employee => new { Name = employee.FirstName + " " + (employee.MiddleName == null ? string.Empty : employee.MiddleName) + " " + employee.LastName, Department = employee.Department.Name, Town = employee.Address.Town.Name }); foreach (var extractedEmployee in extractedEmployees) { container.Append("Name: ").AppendLine(extractedEmployee.Name); container.Append("Department: ").AppendLine(extractedEmployee.Department); container.Append("Town: ").AppendLine(extractedEmployee.Town); container.AppendLine(new string('-', 50)); } Console.WriteLine(container); container.Clear(); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }