static void Main() { var context = new AdsEntities(); Stopwatch stopwatch = new Stopwatch(); context.Database.SqlQuery <string>("CHECKPOINT; DBCC DROPCLEANBUFFERS;"); stopwatch.Start(); var selectAllCoulmns = context.Ads.ToList(); foreach (var ad in selectAllCoulmns) { Console.WriteLine(ad.Title); } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); var selectOnlyTitle = context.Ads.Select(a => a.Title).ToList(); foreach (var ad in selectOnlyTitle) { Console.WriteLine(ad); } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); }
static void Main() { var context = new AdsEntities(); Stopwatch stopwatch = new Stopwatch(); context.Database.SqlQuery <string>("CHECKPOINT; DBCC DROPCLEANBUFFERS;"); stopwatch.Start(); var allAds = context.Ads .ToList() .Where(a => a.AdStatus.Status == "Published") .Select(a => new { a.Title, Category = a.Category.Name, Town = a.Town.Name, a.Date }) .OrderBy(a => a.Date); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); var improvedAllAds = context.Ads .Where(a => a.AdStatus.Status == "Published") .OrderBy(a => a.Date) .Select(a => new { a.Title, Category = a.Category.Name, Town = a.Town.Name }) .ToList(); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); }
static void Main() { var context = new AdsEntities(); // 25 requests without Include(...) var allAds = context.Ads.ToList(); foreach (var ad in allAds) { Console.WriteLine("+-----------{0}Title: {1}{0}Status: {2}{0}Category: {3}{0}Town: {4}{0}User: {5}", "\r\n", ad.Title, ad.AdStatus.Status, (ad.CategoryId == null ? "(no category)" : ad.Category.Name), (ad.TownId == null ? "(no town)" : ad.Town.Name), (ad.AspNetUser.Id == null ? "(no owner)" : ad.AspNetUser.UserName)); } // 1 request without Include(...) var selectedAds = context.Ads .Include(a => a.AdStatus) .Include(a => a.Category) .Include(a => a.Town) .Include(a => a.AspNetUser); foreach (var ad in selectedAds) { Console.WriteLine("+-----------{0}Title: {1}{0}Status: {2}{0}Category: {3}{0}Town: {4}{0}User: {5}", "\r\n", ad.Title, ad.AdStatus.Status, (ad.CategoryId == null ? "(no category)" : ad.Category.Name), (ad.TownId == null ? "(no town)" : ad.Town.Name), (ad.AspNetUser.Id == null ? "(no owner)" : ad.AspNetUser.UserName)); } }