static void Main() { var db = new AdsEntityes(); var ads = db.Ads; foreach (var ad in ads.ToList()) { Console.WriteLine("{0} {1} {2} {3} {4}", ad.Title == null ? "" : ad.Title, ad.AdStatus.Status == null ? "" : ad.AdStatus.Status, ad.AdCategory.Name == null ? "" : ad.AdCategory.Name, ad.Town.Name == null ? "" : ad.Town.Name, ad.AspNetUser.Name == null ? "" : ad.AspNetUser.Name); } db.Ads.Include(ast => ast.AdStatus.Status) .Include(ac => ac.AdCategory.Name) .Include(at => at.Town.Name) .Include(au => au.AspNetUser.Name) .ToList() .ForEach(a => Console.WriteLine(a.Title + " " + a.AdStatus.Status + " " + a.AdCategory.Name + " " + a.Town.Name + " " + a.AspNetUser.Name)); var add = db.Ads.Select(a => a.Title + " " + a.AdStatus.Status + " " + a.AdCategory.Name + " " + a.Town.Name + " " + a.AspNetUser.Name) .ToList(); add.ForEach(ad => Console.WriteLine(ad)); }
//Using Entity Framework select all ads from the database, then //invoke ToList(), then filter the categories by status "Published", //then select ad title, category and town, then invoke ToList() and finally //order the ads by publish date. Rewrite the same in more optimized way and //compare the performance. static void Main(string[] args) { var db = new AdsEntityes(); var ads = db.Ads.ToList() .Where(a => a.AdStatus.Status == "Published") .OrderBy(a => a.Date) .ToList(); ads.ForEach(a => Console.WriteLine("{0} {1} {2}", a.Title == null ? "" : a.Title, a.AdCategory.Name == null ? "" : a.AdCategory.Name, a.Town.Name == null ? "" : a.Town.Name)); db.Ads.Where(a => a.AdStatus.Status == "Published") .Select(a => a.Title + " " + a.AdCategory.Name + " " + a.Town.Name).ToList() .ForEach(a => Console.WriteLine(a)); }
static void Main(string[] args) { var db = new AdsEntityes(); DateTime start = new DateTime(); DateTime stop = new DateTime(); start = DateTime.Now; var adsOne = db.Ads.ToList().Select(a => a.Title).ToList(); adsOne.ForEach(a => Console.WriteLine(a)); stop = DateTime.Now; var result = (start - stop); Console.WriteLine("\n RESULT 1 {0} \n", result); start = DateTime.Now; var adsTwo = db.Ads.Select(a => a.Title).ToList(); adsTwo.ForEach(a => Console.WriteLine(a)); stop = DateTime.Now; result = (start - stop); Console.WriteLine("\n RESULT 2 {0}", result); }