private static void SelectDataWithInclude_Fast(TelerikAcademyEntities dbContext)
 {
     foreach (var employee in dbContext.Employees.Include("Department").Include("Address").Include("Address.Town"))
     {
         Console.WriteLine("Employee name: {0}; Employee department: {1}; Employee town: {2}", employee.FirstName + ' ' + employee.LastName, employee.Department.Name, employee.Address.Town.Name);
     }
 }
        static void Main()
        {
            TelerikAcademyEntities dbContext = new TelerikAcademyEntities();

            // This query without Include will make around 343 queries 
            SelectDataWithoutInclude_Slow(dbContext);

            //// This query with Include will make only 1 query
            //SelectDataWithInclude_Fast(dbContext);
        }
コード例 #3
0
    static void Main()
    {
        TelerikAcademyEntities dbContext = new TelerikAcademyEntities();

        // This query with 3xToList make around 647 queries
        var slowQuery = dbContext.Employees.ToList()
            .Select(emp => emp.Address).ToList()
            .Select(t => t.Town).ToList()
            .Where(t => t.Name == "Sofia");

        //This query with only 1xToList make only 1 query
        var fastQuery = dbContext.Employees
            .Select(emp => emp.Address)
            .Select(t => t.Town)
            .Where(t => t.Name == "Sofia").ToList();
    }