static void Main() { string decorationLine = new string('-', Console.WindowWidth); Console.Write(decorationLine); Console.WriteLine("***Selecting all employees and printing their name, department and town***"); Console.Write(decorationLine); // Without '.Include()' -> 340 SQL statements executed Console.WriteLine("---Getting the employees (slow version)---"); using (TelerikAcademyEntities context = new TelerikAcademyEntities()) { var employees = context.Employees; foreach (Employee employee in employees) { Console.WriteLine("Name: {0} {1}; Department: {2}; Town: {3}", employee.FirstName, employee.LastName, employee.Department.Name, employee.Address.Town.Name); } } Console.WriteLine(); // With '.Include()' -> 1 SQL statement executed Console.WriteLine("---Getting the employees (fast version)---"); using (TelerikAcademyEntities context = new TelerikAcademyEntities()) { var employees = context.Employees.Include("Department").Include("Address.Town"); foreach (var employee in employees) { Console.WriteLine("Name: {0} {1}; Department: {2}; Town: {3}", employee.FirstName, employee.LastName, employee.Department.Name, employee.Address.Town.Name); } } }
static void Main() { string decorationLine = new string('-', Console.WindowWidth); Console.Write(decorationLine); Console.WriteLine("***Getting information about the employees from Sofia***"); Console.Write(decorationLine); // With '.ToList()' many times -> ~1000 SQL statements executed Console.WriteLine("---Getting the information (slow version)---"); using (TelerikAcademyEntities context = new TelerikAcademyEntities()) { var employeesFromSofia = context.Employees.ToList() .Select(e => e.Address).ToList() .Select(a => a.Town).ToList() .Where(t => t.Name == "Sofia"); Console.WriteLine("The employees from 'Sofia' are: {0}", employeesFromSofia.Count()); } Console.WriteLine(); // Without '.ToList()' -> 1 SQL statement executed Console.WriteLine("---Getting the information (fast version)---"); using (TelerikAcademyEntities context = new TelerikAcademyEntities()) { var employeesFromSofia = from employee in context.Employees where employee.Address.Town.Name == "Sofia" select employee; foreach (var employee in employeesFromSofia) { Console.WriteLine("{0} {1}", employee.FirstName, employee.LastName); } } }