private static void AdsWithoutInclude(AdsEntities db) { foreach (var ad in db.Ads) { Console.WriteLine("{0} {1} {2} {3} {4}", ad.Title, ad.AdStatus.Status, (ad.CategoryId == null) ? "no data" : ad.Category.Name, (ad.TownId == null) ? "no town" : ad.Town.Name, ad.AspNetUser.Name); } }
static void Main() { var db = new AdsEntities(); var stopWatch = new Stopwatch(); stopWatch.Start(); SlowQuery(db); Console.WriteLine("Slow query: " + stopWatch.Elapsed); stopWatch.Restart(); OptimizedQuery(db); Console.WriteLine("Optimized query: " + stopWatch.Elapsed); }
private static void SlowQuery(AdsEntities db) { var ads = db.Ads .ToList() .Where(a => a.AdStatus.Status == "Published") .OrderBy(a => a.Date) .SelectMany(a => new[] { a.Title, (a.CategoryId == null) ? "no category" : a.Category.Name, (a.TownId == null) ? "no town" : a.Town.Name, }).ToList(); //for (int i = 0; i < ads.Count; i += 3) //{ // Console.WriteLine("{0} {1} {2}", ads[i], ads[i + 1], ads[i + 2]); //} }
private static void OptimizedQuery(AdsEntities db) { var ads = db.Ads .Where(a => a.AdStatus.Status == "Published") .OrderBy(a => a.Date) .Include(a => a.Category) .Include(a => a.Town) .ToList(); //foreach (var ad in ads) //{ // Console.WriteLine("{0} {1} {2}", // ad.Date, // (ad.CategoryId == null) ? "no category" : ad.Category.Name, // (ad.TownId == null) ? "no town" : ad.Town.Name); //} }
static void Main() { var db = new AdsEntities(); var stopWatch = new Stopwatch(); stopWatch.Start(); var selectAllFromAds = db.Ads; foreach (var selectAllFromAd in selectAllFromAds.Take(3)) { Console.WriteLine(selectAllFromAd.Title); } Console.WriteLine("Select all from ads: " + stopWatch.Elapsed); stopWatch.Restart(); var selectTitleFromAds = db.Ads.Select(a => a.Title); foreach (var selectTitleFromAd in selectTitleFromAds.Take(3)) { Console.WriteLine(selectTitleFromAd); } Console.WriteLine("Select title from ads: " + stopWatch.Elapsed); }
static void Main() { var db = new AdsEntities(); AdsWithoutInclude(db); }
public static void Main() { var db = new AdsEntities(); var adsCount = db.Ads.Count(); }