static void Main(string[] args) { TelerikAcademyEntities context = new TelerikAcademyEntities(); Stopwatch sw = new Stopwatch(); sw.Start(); var employees = context.Employees; foreach (var employee in employees) { Console.WriteLine("{0} {1} {2} from {4} works in {3}", employee.FirstName,employee.MiddleName, employee.LastName, employee.Department.Name, employee.Address.Town.Name); } sw.Stop(); Console.WriteLine("Without Include done in {0}", sw.Elapsed); Console.WriteLine("-----------------------------------------"); sw.Restart(); employees = context.Employees; foreach (var employee in employees.Include("Department").Include("Address.Town")) { Console.WriteLine("{0} {1} {2} from {4} works in {3}", employee.FirstName, employee.MiddleName, employee.LastName, employee.Department.Name, employee.Address.Town.Name); } sw.Stop(); Console.WriteLine("With Include done in {0}", sw.Elapsed); }
private static void SelectEmployees() { using (var context = new TelerikAcademyEntities()) { Console.WriteLine("Employees:"); var employees = from e in context.Employees join a in context.Addresses on e.AddressID equals a.AddressID join t in context.Towns on a.TownID equals t.TownID where t.Name == "Sofia" select e; foreach (var employee in employees.ToList()) { Console.WriteLine( "Name: {0} {1}\nAddress: {2}\nTown: {3}", employee.FirstName, employee.LastName, employee.Address.AddressText, employee.Address.Town.Name); } } }
private static void SelectEmployeesUsingToList() { using (var context = new TelerikAcademyEntities()) { Console.WriteLine("Employees:"); var employees = context.Employees.ToList() .Select(e => new { FirstName = e.FirstName, LastName = e.LastName, Address = e.Address.AddressText, TownName = e.Address.Town.Name }) .ToList() .Where(e => e.TownName == "Sofia") .ToList(); foreach (var employee in employees) { Console.WriteLine( "Name: {0} {1}\nAddress: {2}\nTown: {3}", employee.FirstName, employee.LastName, employee.Address, employee.TownName); } } }
private static void SelectEmployeesNoInclude() { using (var context = new TelerikAcademyEntities()) { Console.WriteLine("Employees:"); foreach (var employee in context.Employees) { Console.WriteLine( "Name: {0} {1}\nDepartment: {2}\nTown: {3}", employee.FirstName, employee.LastName, employee.Department.Name, employee.Address.Town.Name); } } }
public static void Main() { using (var telerikAcademy = new TelerikAcademyEntities()) { // More than 300 queries foreach (var employee in telerikAcademy.Employees) { Console.WriteLine("{0} | {1} | {2}", employee.FirstName + " " + employee.LastName, employee.Department.Name, employee.Address.Town.Name); } //// 1 query //foreach (var employee in telerikAcademy.Employees.Include("Department").Include("Address.Town")) //{ // Console.WriteLine("{0} | {1} | {2}", employee.FirstName + " " + employee.LastName, employee.Department.Name, employee.Address.Town.Name); //} } }
public static void Main() { using (var telerikAcademy = new TelerikAcademyEntities()) { // More than 500 queries var employees = telerikAcademy.Employees.ToList(). Select(x => x.Address).ToList(). Select(x => x.Town).ToList(). Where(x => x.Name == "Sofia").ToList(); //// 1 query //var employees = telerikAcademy.Employees. // Select(x => x.Address). // Select(x => x.Town). // Where(x => x.Name == "Sofia").ToList(); } }
static void Main(string[] args) { TelerikAcademyEntities context = new TelerikAcademyEntities(); string townName = "Sofia"; Stopwatch sw = new Stopwatch(); sw.Start(); var employeesTowns = context.Employees.ToList() .Select(x => x.Address).ToList() .Select(x => x.Town).ToList() .Where(x => x.Name == townName).ToList(); foreach (var town in employeesTowns) { Console.WriteLine(town.Name); } sw.Stop(); Console.WriteLine("Query with many ToList invokations done in {0}", sw.Elapsed); Console.WriteLine("-----------------------------------------------------------"); sw.Restart(); employeesTowns = context.Employees .Select(x => x.Address) .Select(x => x.Town) .Where(x => x.Name == townName).ToList(); foreach (var town in employeesTowns) { Console.WriteLine(town.Name); } sw.Stop(); Console.WriteLine("Query with one ToList invokation in the end done in {0}", sw.Elapsed); }
private static void SelectEmployeesWithStrategy() { using (var context = new TelerikAcademyEntities()) { var strategy = new FetchStrategy(); strategy.LoadWith((Employee e) => e.Department); strategy.LoadWith((Employee e) => e.Address); strategy.LoadWith((Address a) => a.Town); context.FetchStrategy = strategy; foreach (var employee in context.Employees) { Console.WriteLine( "Name: {0} {1}\nDepartment: {2}\nTown: {3}", employee.FirstName, employee.LastName, employee.Department.Name, employee.Address.Town.Name); } } }