static void GetInfoWithInclude(TelerikAcademyEntities dbContext) { foreach (var employee in dbContext.Employees.Include("Address.Town").Include("Department")) { Console.WriteLine("{0} {1}", employee.FirstName, employee.LastName); Console.WriteLine("{0}", employee.Department.Name); Console.WriteLine("{0}", employee.Address.Town.Name); Console.WriteLine(); } }
static void Main() { TelerikAcademyEntities dbContext = new TelerikAcademyEntities(); using (dbContext) { Stopwatch sw = new Stopwatch(); sw.Start(); //GetInfoWithInclude(dbContext); sw.Stop(); var withInclude = sw.Elapsed; sw.Reset(); sw.Start(); GetInfoWithoutInclude(dbContext); sw.Stop(); var withoutInclude = sw.Elapsed; Console.WriteLine("Elapsed time with include: " + withInclude); Console.WriteLine("Elapsed time without include: " + withoutInclude); //using the SQL Server Profiler for SQL Management Studio 2014: //with include - 1 //without include - there are tens of queries in the profiler log } }
static void Main() { var dbContext = new TelerikAcademyEntities(); var sw = new Stopwatch(); sw.Start(); var allEmployees = dbContext.Employees.ToList() .Select(e => e.Address).ToList() .Select(e => e.Town).ToList() .Where(e => e.Name == "Sofia"); sw.Stop(); var withTolist = sw.Elapsed; sw.Reset(); sw.Start(); var allEmployeesOptimized = dbContext.Employees .Select(e => e.Address) .Select(e => e.Town) .Where(e => e.Name == "Sofia").ToList(); sw.Stop(); var withoutToList = sw.Elapsed; Console.WriteLine("Elapsed time with ToList: " + withTolist); Console.WriteLine("Elapsed time without ToList: " + withoutToList); }