public static void Main()
        {
            var adsEntities = new AdsEntities();
            var ads = adsEntities.Ads;

            /*foreach (var ad in ads)
            {
                Console.WriteLine("Title: {0} ({1})", ad.Title, ad.AdStatus.Status);
                Console.WriteLine("Category: {0}", ad.Category == null ? "(no category)" : ad.Category.Name);
                Console.WriteLine("Town: {0}", ad.Town == null ? "(no town)" : ad.Town.Name);
                Console.WriteLine("User: {0}", ad.AspNetUser.Name);
                Console.WriteLine();
            }*/

            foreach (var ad in ads.Include(a => a.AdStatus)
                        .Include(a => a.Town)
                        .Include(a => a.Category)
                        .Include(a => a.AspNetUser))
            {
                Console.WriteLine("Title: {0} ({1})", ad.Title, ad.AdStatus.Status);
                Console.WriteLine("Category: {0}", ad.Category == null ? "(no category)" : ad.Category.Name);
                Console.WriteLine("Town: {0}", ad.Town == null ? "(no town)" : ad.Town.Name);
                Console.WriteLine("User: {0}", ad.AspNetUser.Name);
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            // Without includes
            var adsEntities = new AdsEntities();

            //foreach (var ad in adsEntities.Ads)
            //{
            //    Console.WriteLine("Title: " + ad.Title + ", Status: " + ad.AdStatus.Status +
            //                      ", Category: " + (ad.Category == null ? "(no category)" : ad.Category.Name) +
            //                      ", Town: " + (ad.Town == null ? "(no town)" : ad.Town.Name) +
            //                      ", User: "******"Title: " + ad.Title + ", Status: " + ad.AdStatus.Status +
                    ", Category: " + (ad.Category == null ? "(no category)" : ad.Category.Name) +
                    ", Town: " + (ad.Town == null ? "(no town)" : ad.Town.Name) +
                    ", User: " + ad.AspNetUser.Name);
            }

        }
示例#3
0
        static void Main(string[] args)
        {
            AdsEntities context = new AdsEntities();

            //DataFromRealtedTables(context);

            PlayWithToList(context);
        }
示例#4
0
 public static List <Ad> WithInclude(AdsEntities db)
 {
     return
         (db.Ads.Include(a => a.AdStatus)
          .Include(a => a.AspNetUser)
          .Include(a => a.Category)
          .Include(a => a.Town)
          .ToList());
 }
示例#5
0
        public static void Main(string[] args)
        {
            #region Problem 1.	Show Data from Related Tables
            using (var db = new AdsEntities())
            {
                ConsolePrintAds(DataFromRelatedTables.WithoutInclude(db));
            }

            using (var db = new AdsEntities())
            {
                ConsolePrintAds(DataFromRelatedTables.WithInclude(db));
            }
            #endregion

            #region Problem 2.	Play with ToList()

            using (var db = new AdsEntities())
            {
                var adsSlow = db.Ads.ToList()
                              .Where(a => a.AdStatus.Status == "Published")
                              .OrderBy(a => a.Date)
                              .ToList()
                              .Select(a => new { a.Title, a.Category, a.Town });
            }


            using (var db = new AdsEntities())
            {
                var adsSlow = db.Ads
                              .Where(a => a.AdStatus.Status == "Published")
                              .OrderBy(a => a.Date)
                              .Select(a => new { a.Title, a.Category, a.Town })
                              .ToList();
            }

            #endregion

            #region Problem 3.	Select Everything vs. Select Certain Columns

            using (var db = new AdsEntities())
            {
                var adsTitle = db.Ads.Select(a => new { a.Title }).ToList();
            }


            using (var db = new AdsEntities())
            {
                var adsTitle = db.Ads.ToList();
            }

            #endregion
        }
        static void Main(string[] args)
        {
            var adsEntities = new AdsEntities();

            // Slow sql
            //var slowSQLQuery = adsEntities.Ads
            //    .ToList()
            //    .Where(a => a.AdStatus.Status == "Published")
            //    .OrderBy(a => a.Date)
            //    .Select(s =>
            //        new
            //        {
            //            Title = s.Title,
            //            Category = s.Category,
            //            Town = s.Town
            //        }
            //    ).ToList();

            //foreach (var ad in slowSQLQuery)
            //{
            //    Console.WriteLine("Title: " + ad.Title + 
            //        ", Category: " + (ad.Category == null ? "(no category)" : ad.Category.Name) +
            //        ", Town: " + (ad.Town == null ? "(no category)" : ad.Town.Name));
            //}

            // Optimized Query
            var optimizedQuery = adsEntities.Ads
                .Where(a => a.AdStatus.Status == "Published")
                .OrderBy(a => a.Date)
                .Select(s =>
                    new
                    {
                        Title = s.Title,
                        Category = s.Category,
                        Town = s.Town
                    }
                );

            foreach (var ad in optimizedQuery)
            {
                Console.WriteLine("Title: " + ad.Title +
                                  ", Category: " + (ad.Category == null ? "(no category)" : ad.Category.Name) +
                                  ", Town: " + (ad.Town == null ? "(no category)" : ad.Town.Name));
            }


        }
示例#7
0
        static void Main(string[] args)
        {
            var adsEntities = new AdsEntities();

            
            //var allAdsColumns = adsEntities.Ads;
            //foreach (var ad in allAdsColumns)
            //{
            //    Console.WriteLine("Title: " + ad.Title);
            //}

            var adTitles = adsEntities.Ads.Select(a => a.Title);
            foreach (var adTitle in adTitles)
            {
                Console.WriteLine("Title: " + adTitle);
            }
        }
示例#8
0
        private static void PlayWithToList(AdsEntities context)
        {
            var ads = context.Ads
                      .ToList()
                      .Where(a => a.AdStatus.Status.Equals("Published"))
                      .Select(a => new
            {
                title    = a.Title,
                category = a.Category.Name,
                town     = a.Town.Name,
                date     = a.Date
            })
                      .ToList()
                      .OrderBy(a => a.date);

            Console.WriteLine(ads);
        }
示例#9
0
        private static void DataFromRealtedTables(AdsEntities context)
        {
            // Without include = 1 query
            //            var ads = context.Ads.Select(a => new
            //            {
            //                title = a.Title,
            //                status = a.AdStatus.Status,
            //                category = a.Category.Name,
            //                town = a.Town.Name,
            //                user = a.AspNetUser.Name
            //            });
            //


            // With include - 1 query

            var ads = context.Ads.Include("Categories")
                      .Include("AdStatuses")
                      .Include("Towns")
                      .Include("AspNetUsers")
                      .Select(a => new
            {
                title    = a.Title,
                status   = a.AdStatus.Status,
                category = a.Category.Name,
                town     = a.Town.Name,
                user     = a.AspNetUser.Name
            });

            foreach (var ad in ads)
            {
                Console.WriteLine(ad.title);
                Console.WriteLine(ad.status);
                Console.WriteLine(ad.category);
                Console.WriteLine(ad.town);
                Console.WriteLine(ad.user);
                Console.WriteLine("-----------------");
            }
        }
示例#10
0
 public static List <Ad> WithoutInclude(AdsEntities db)
 {
     return(db.Ads.ToList());
 }