public static void Main()
        {
            var context = new AdsEntities();

            TimeSpan[] nonOptimizedTestDurations = new TimeSpan[10];
            TimeSpan[] optimizedTestDurations = new TimeSpan[10];

            Console.WriteLine("Non-optimized:");
            for (int i = 0; i < 10; i++)
            {
                nonOptimizedTestDurations[i] = GetTimeForNonOptimizedSelect(context);
                Console.WriteLine("Run {0}: {1}", i + 1, nonOptimizedTestDurations[i]);
            }

            Console.WriteLine("\nOptimized:");
            for (int i = 0; i < 10; i++)
            {
                optimizedTestDurations[i] = GetTimeForOptimizedSelect(context);
                Console.WriteLine("Run {0}: {1}", i + 1, optimizedTestDurations[i]);
            }

            Console.WriteLine("\nNon-optimized avarage: {0} Ticks{1}Optimized avarage: {2} Ticks",
                nonOptimizedTestDurations.Average(n => n.Ticks),
                    Environment.NewLine,
                    optimizedTestDurations.Average(n => n.Ticks));
        }
        public static void Main()
        {
            var context = new AdsEntities();

            ListAllAdsWithoutInclude(context);

            //ListAllAdsWithInclude(contex);
        }
 private static void ListAllAdsWithoutInclude(AdsEntities contex)
 {
     foreach (var ad in contex.Ads)
     {
         Console.WriteLine("Ad Name: {0}{5}Status: {1}{5}Category: {2}{5}Town: {3}{5}User: {4}{5}",
             ad.Title,
             ad.AdStatus.Status,
             ad.Category == null ? string.Empty : ad.Category.Name,
             ad.Town == null ? string.Empty : ad.Town.Name,
             ad.AspNetUser.UserName,
             Environment.NewLine);
     }
 }
        private static TimeSpan GetTimeForOptimizedSelect(AdsEntities context)
        {
            context.Database.SqlQuery<Ad>("CHECKPOINT; DBCC DROPCLEANBUFFERS;");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            var ads = context.Ads.Select(a => a.Title);
            string tempAdName;

            foreach (var ad in ads)
            {
                tempAdName = ad;
            }

            var timeSpent = stopwatch.Elapsed;
            stopwatch.Reset();

            return timeSpent;
        }
        private static void ListAllAdsWithInclude(AdsEntities contex)
        {
            var ads = contex.Ads
                .Include(a => a.Category)
                .Include(a => a.Town)
                .Include(a => a.AdStatus)
                .Include(a => a.AspNetUser);

            foreach (var ad in ads)
            {
                Console.WriteLine("Ad Name: {0}{5}Status: {1}{5}Category: {2}{5}Town: {3}{5}User: {4}{5}",
                    ad.Title,
                    ad.AdStatus.Status,
                    ad.Category == null ? string.Empty : ad.Category.Name,
                    ad.Town == null ? string.Empty : ad.Town.Name,
                    ad.AspNetUser.UserName,
                    Environment.NewLine);
            }

            //Second way
            //var ads = contex.Ads
            //    .Select(a => new
            //    {
            //        Title = a.Title,
            //        Status = a.AdStatus.Status,
            //        Category = a.Category.Name,
            //        Town = a.Town.Name,
            //        User = a.AspNetUser.UserName
            //    });

            //foreach (var ad in ads)
            //{
            //    Console.WriteLine("Ad Name: {0}{5}Status: {1}{5}Category: {2}{5}Town: {3}{5}User: {4}{5}",
            //        ad.Title,
            //        ad.Status,
            //        ad.Category,
            //        ad.Town,
            //        ad.User,
            //        Environment.NewLine);
            //}
        }
        private static TimeSpan ListAdsOptimizedVersion(AdsEntities context)
        {
            context.Database.SqlQuery<Ad>("CHECKPOINT; DBCC DROPCLEANBUFFERS;");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            var ad = context.Ads
                .Where(a => a.AdStatus.Status.Equals("Published"))
                .OrderBy(a => a.Date)
                .Select(a => new
                {
                    AdTitle = a.Title,
                    AdCategory = a.Category == null ? string.Empty : a.Category.Name,
                    AdTown = a.Town == null ? string.Empty : a.Town.Name,
                    AdPublishedOn = a.Date
                })
                .ToList();

            var timeSpent = stopwatch.Elapsed;
            stopwatch.Reset();

            return timeSpent;
        }
Exemplo n.º 7
0
        static void Main()
        {
            var db    = new AdsEntities();
            var start = db.Ads.Count();
            var ads   = db.Ads
                        .Include(a => a.Category)
                        .Include(a => a.AdStatus)
                        .Include(a => a.Town)
                        .Include(a => a.AspNetUser);

            foreach (var ad in ads)
            {
                if (ad.Category == null)
                {
                    ad.Category = new Category()
                    {
                        Name = "Null"
                    };
                }

                if (ad.Town == null)
                {
                    ad.Town = new Town()
                    {
                        Name = "Null"
                    };
                }

                Console.WriteLine("Title: {0}, Status: {1}, Category: {2}, Town: {3}, User: {4}",
                                  ad.Title,
                                  ad.AdStatus.Status,
                                  ad.Category.Name,
                                  ad.Town.Name,
                                  ad.AspNetUser.UserName);
            }
        }