Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            using (LibraryDb ctx = new LibraryDb())
            {
                var res1 = ctx.Books.Count().ToString();  // 2
                //MessageBox.Show(res1);
                Debug.WriteLine(res1);

                var res2 = ctx.Books.Where(b => b.PagesQuantity > 500);
                Debug.WriteLine(res2.ToList());
                //MessageBox.Show(res2.ToList().ToString());

                var res3 = ctx.Books
                           .Count(b => b.PagesQuantity > 500);

                #region workAbleCode Part1
                //var myList = new List<SimpleBook>();
                //foreach (var item in ctx.Books.Include("Authors"))
                //{
                //    var authors = item.Authors;
                //    string container = String.Empty;
                //    foreach (var str in authors)
                //    {
                //        container += (str.Name+", ");
                //    }
                //    myList.Add(new SimpleBook
                //    {
                //        Title = item.Title,
                //        Authors = container
                //    });
                //}
                //foreach (var item in myList)
                //{
                //    MessageBox.Show(item.ToString());
                //}
                #endregion

                //10 lines
                #region workAbleCode Part2
                ctx.Books
                .ToList()
                .Select(b => new SimpleBook
                {
                    Title   = b.Title,
                    Authors = b.Authors
                              .Aggregate("", (prev, next) => prev + next.Name + ", ")
                })
                .ToList()
                .ForEach(elem => MessageBox.Show(elem.ToString()));
                #endregion
            }
        }
        protected override void Seed(LibraryDb context)
        {
            var genre = new Genre
            {
                Name        = "SciFi",
                Description = "Scince " +
                              "Fiction. Fiction based on imagined future scientific or technological " +
                              "advances and major social or environmental " +
                              "changes, frequently portraying space or time travel and " +
                              "life on other planets"
            };

            context.Genres.Add(genre);
            var language = new Language {
                Name = "English"
            };

            context.Authors.Add(new Author
            {
                Name    = "Douglas Adams",
                Country = new Country {
                    Name = "England"
                },
                BirthDate = new System.DateTime(1952, 3, 11),
                DeathDate = new System.DateTime(2001, 4, 11),
                Sex       = new Sex {
                    Name = "Male"
                },
                Books = new List <Book> {
                    new Book {
                        Genre         = genre,
                        Language      = language,
                        PagesQuantity = 1000,
                        PublishDate   = new System.DateTime(1979, 1, 1),
                        Title         = "The Restaurant at the End of The Universe"
                    },
                    new Book {
                        Genre         = genre,
                        Language      = language,
                        PagesQuantity = 200,
                        PublishDate   = new System.DateTime(1979, 1, 1),
                        Title         = "SQL in 45 minutes"
                    }
                }
            });

            context.SaveChanges();
        }