public IHttpActionResult GetUserPurchases([FromUri]string username)
        {
            using (var bookShopContext = new BookShopContext())
            {
                var user = bookShopContext.Users.First(u => u.UserName == username);

                if (user == null)
                {
                    return this.NotFound();
                }

                var result = new
                {
                    username = username,
                    purchases = from purchase in user.Purchases
                                select new
                                {
                                    bookTitle = purchase.Book.Title,
                                    price = purchase.Price,
                                    dateOfPurchase = purchase.DateOfPurchase,
                                    isRecalled = purchase.IsRecalled
                                }
                };

                return this.Ok(result);
            }
        }
        public IHttpActionResult RemoveUserRole([FromUri] string username, [FromBody] string roleName)
        {
            using (var context = new BookShopContext())
            {
                if (!context.Users.Any(u => u.UserName == username))
                {
                    return this.NotFound();
                }

                var user = context.Users.First(u => u.UserName == username);

                if (!context.Roles.Any(r => r.Name == roleName))
                {
                    return this.NotFound();
                }

                var role = context.Roles.First(r => r.Name == roleName);

                var identityUserRole = role.Users.First(u => u.UserId == user.Id);

                role.Users.Remove(identityUserRole);

                context.SaveChanges();

                return this.Ok();
            }
        }
示例#3
0
        public static void AuthorsWithBookBefore1990(BookShopContext context)
        {
            var result = context.Books.Where(x => x.ReleaseDate.Year < 1990);

            foreach (var book in result)
            {
                Console.WriteLine($"{book.Author.FirstName} {book.Author.LastName}");
            }
        }
示例#4
0
        public static void BooksAfter2000(BookShopContext context)
        {
            var result = context.Books.Where(x => x.ReleaseDate.Year > 2000);

            foreach (var book in result)
            {
                Console.WriteLine($"{book.Title}");
            }
        }
示例#5
0
        static void Main()
        {
            var migrationStrategy = new MigrateDatabaseToLatestVersion<BookShopContext, Configuration>();

            Database.SetInitializer(migrationStrategy);

            var context = new BookShopContext();

            var bookCount = context.Books.Count(); //- to insialize the DB
        }
示例#6
0
        static void Main()
        {
            var context = new BookShopContext();

            //var booksAfter2000 = context.Books
            //    .Where(b => b.ReleaseDate.Value.Year > 2000)
            //    .Select(b => new
            //    {
            //        b.Title,
            //        b.ReleaseDate.Value.Year
            //    });
            //foreach (var book in booksAfter2000)
            //{
            //    Console.WriteLine(book);
            //}

            //var authorsWithOldBooks = context.Authors
            //    .Where(a => a.Books.Any(
            //        b => b.ReleaseDate.Value.Year < 1990))
            //    .Select(a => new
            //    {
            //        a.FirstName,
            //        a.LastName
            //    });
            //foreach (var author in authorsWithOldBooks)
            //{
            //    Console.WriteLine(author);
            //}

            //var authorsOrderedByBooks = context.Authors
            //    .OrderByDescending(a => a.Books.Count())
            //    .Select(a => new
            //    {
            //        a.FirstName,
            //        a.LastName,
            //        BookCount = a.Books.Count()
            //    });
            //foreach (var author in authorsOrderedByBooks)
            //{
            //    Console.WriteLine(author);
            //}

            //var booksForSpecificAuthor = context.Authors
            //    .Where(a => a.FirstName == "George" &&
            //                a.LastName == "Powell")
            //    .OrderByDescending(
            //        a => a.Books.Any(b => b.ReleaseDate.Value.Year > 0))
            //    .ThenBy(a => a.Books.Any(b => b.Title != ""))
            //    .Select(a => a.Books.Any(b => new
            //    {
            //        b.Title,
            //        b.ReleaseDate,
            //        b.Copies
            //    }));
        }
示例#7
0
        public static void BookFromSpecificAuthor(BookShopContext context, string authorName)
        {
            string[] authorNames = authorName.Split(new []{' '});
            string firstName = authorNames[0];
            string secondName = authorNames[1];
            var books = context.Books
                .Where(x => x.Author.FirstName==firstName && x.Author.LastName==secondName)
                .OrderByDescending(x => x.ReleaseDate)
                .OrderBy(x => x.Title);

            foreach (var book in books)
            {
                Console.WriteLine($"{book.Title} {book.ReleaseDate} {book.Copies}");
            }
        }
        static void Main()
        {
         
            var context = new BookShopContext();

            var bookCount = context.Books.Count(); 
            Console.WriteLine(bookCount);

            var books = context.Books.Where(b => b.AuthorId == 1).ToList();

            var authorById = context.Authors.FirstOrDefault(a => a.Id == 1);

            Console.WriteLine(authorById.FirstName);

        }
示例#9
0
        static void Main()
        {
            var context=new BookShopContext();

            var migrationStrategy = new MigrateDatabaseToLatestVersion<BookShopContext,Configuration>();
            Database.SetInitializer(migrationStrategy);
            context.Database.Initialize(true);

              //  BooksAfter2000(context);

              //  AuthorsWithBookBefore1990(context);

              //  BookFromSpecificAuthor(context,"George Powell");

             //   TestBookRealtions(context);
        }
        static void Main()
        {
            var context = new BookShopContext();
            var booksCount = context.Books.Count();
            //1.	Get all books after the year 2000. Select only their titles.

            var booksAfter2000 = context.Books
                .Where(b => b.ReleaseDate >= new DateTime(2000, 1, 1))
                .Select(b => new
                {
                    title = b.Title
                }).ToList();
            //booksAfter2000.ForEach(Console.WriteLine);

            //2.	Get all authors with at least one book with release date before 1990. Select their first name and last name

            var authors = context.Authors
                .Where(a => a.Books
                    .Any(b => b.ReleaseDate <= new DateTime(1999, 1, 1)))
                .Select(a => new
                {
                    FirstName = a.FirstName,
                    LastName = a.LastName
                }).ToList();
            //foreach (var author in authors)
            //{
            //    Console.WriteLine("First Name: {0} " +
            //                      "Last Name: {1}",
            //        author.FirstName,
            //        author.LastName);
            //}

            //3.	Get all authors, ordered by the number of their books (descending). Select their first name, last name and book count.
            var authorBooks = context.Authors
                .OrderByDescending(a=>a.Books.Count)
                .Select(a => new
                {
                    a.FirstName,
                    a.LastName,
                    a.Books.Count
                });
            //foreach (var authorBook in authorBooks)
            //{
            //    Console.WriteLine(authorBook);
            //}
        }
 private static void BooksFromAuthorName(BookShopContext db)
 {
     var books = db.Books
         .Where(b => b.Author.FirstName == "George" && b.Author.LastName == "Powell")
         .OrderByDescending(b => b.ReleaseDate).ThenBy(b => b.Title)
         .Select(b => new
         {
             b.Title,
             b.ReleaseDate,
             b.Copies,
             AuthorName = b.Author.FirstName + " " + b.Author.LastName
         });
     foreach (var book in books)
     {
         Console.WriteLine(book.Title + " " + book.ReleaseDate + " " + book.ReleaseDate + " " + book.AuthorName);
     }
 }
示例#12
0
        static void Main()
        {
            var db = new BookShopContext();

            var books = db.Books
                .Take(3)
                .ToList();

            foreach (var b in books)
            {
                Console.WriteLine(b.Title);

                foreach (var b2 in b.RelatedBooks)
                {
                    Console.WriteLine("\t" + b2.Title);
                }
            }
        }
        private static void RelatedBooksByCategory(BookShopContext db)
        {
            var categories = db.Categories
                .Select(c => new
                {
                    CategoryName = c.Name,
                    BookCount = c.Books.Count,
                    Books =
                        c.Books.Select(b => new {b.Title, b.ReleaseDate})
                            .OrderByDescending(b => b.ReleaseDate)
                            .ThenBy(b => b.Title)
                }).OrderByDescending(c => c.BookCount);

            foreach (var category in categories)
            {
                Console.WriteLine("--"+category.CategoryName + ", " + category.BookCount +" books");
                var books = category.Books;
                foreach (var book in books)
                {
                    Console.WriteLine("         " + book.Title + " " +"("+ book.ReleaseDate.Value.Year+")");
                }
            }
        }
        static void Main(string[] args)
        {
            //1.First we make the models - BookShopSystem.Models - contains all the classes with theyr properties (class library) 
            //2.1. Then we make the data layer - in it we insert the code first model 
            //2.2. import references (from models) and inset DbSets from the models ( class library)-->public virtual dbset<author>..
            //2.3. then we fix the app.config file - change the connection string -initial catalog and data source
            //3.1. then we make ...Console client (Console Application) and reference the Models and the Data 
            //3.2. in it we also install Entity Framework thrue Nuget Package Manager 
            //3.3. we copy the connection string from the ...Data model and paste it in the app.config in ConsoleClient
            //3.4. finally to create the database we must execute any action thrue the context
            // * to make restrictions we must add reference = > .. annotation 


            var db = new BookShopContext();

            //1.	Get all books after the year 2000. Select only their titles.

            //BooksReleasedAfter2000(db);

            //2.	Get all authors with at least one book with release date before 1990. Select their first name and last name.

            //FilterAuthorsByBookCountAndReleaseDate(db);

            //3.	Get all authors, ordered by the number of their books (descending).
            //Select their first name, last name and book count.

            //AuthorsOrderedByBookCount(db);

            //4.   Get all books from author George Powell, ordered by their release date (descending),
            //then by book title (ascending).Select the book's title, release date and copies.

            //BooksFromAuthorName(db);

            //5.  Get the most recent books by categories. The categories should be ordered by total book count. Only take the top 3 most recent books from each category - ordered by date (descending), then by title (ascending). Select the category name, total book count and for each book - its title and release date.

            RelatedBooksByCategory(db);
        }
示例#15
0
        public static void TestBookRealtions(BookShopContext context)
        {
            var books = context.Books
                               .Take(3)
                               .ToList();

            books[0].RelatedBooks.Add(books[1]);
            books[1].RelatedBooks.Add(books[0]);
            books[0].RelatedBooks.Add(books[2]);
            books[2].RelatedBooks.Add(books[0]);

            context.SaveChanges();

            var booksFromQuery = context.Books.Take(3);

            foreach (var book in booksFromQuery)
            {
                Console.WriteLine("--{0}", book.Title);
                foreach (var relatedBook in book.RelatedBooks)
                {
                    Console.WriteLine(relatedBook.Title);
                }
            }
        }
        public static void Main()
        {
            var context = new BookShopContext();
            var bookCount = context.Books.Count();

            // Problem 06 - 01
            var books = context.Books
                .Where(b => b.ReleaseDate.Year > 2000)
                .Select(b => b.Title);

            foreach (var book in books)
            {
                Console.WriteLine(book);
            }

            Console.WriteLine();

            // Problem 06 - 02
            var authors = context.Authors
                .Where(a => a.Books.Any(b => b.ReleaseDate.Year < 1990))
                .Select(a => new
                    {
                        a.FirstName,
                        a.LastName
                    });

            foreach (var author in authors)
            {
                Console.WriteLine(author.FirstName + " " + author.LastName);
            }

            Console.WriteLine();

            // Problem 06 - 03
            var authorsByBooksCount = context.Authors
                .OrderByDescending(a => a.Books.Count)
                .Select(a => new
                    {
                        a.FirstName,
                        a.LastName,
                        a.Books.Count
                    });

            foreach (var author in authorsByBooksCount)
            {
                Console.WriteLine(author.FirstName + " " + author.LastName + ", " + author.Count);
            }

            Console.WriteLine();

            // Problem 06 - 04
            var booksByGivenAuthor =
                context.Books.Where(b => b.Author.FirstName == "George" && b.Author.LastName == "Powell")
                    .OrderByDescending(b => b.ReleaseDate)
                    .ThenBy(b => b.Title)
                    .Select(b => new
                        {
                            b.Title,
                            b.ReleaseDate,
                            b.Copies
                        });

            foreach (var book in booksByGivenAuthor)
            {
                Console.WriteLine(book.Title + ", " + book.ReleaseDate + ", " + book.Copies);
            }

            Console.WriteLine();

            // Problem 06 - 05
            var categoriesRecentBooks =
                context.Categories.OrderByDescending(c => c.Books.Count)
                    .Select(c => new
                        {
                            Category = c.Name,
                            BooksCount = c.Books.Count,
                            Books = c.Books
                                .OrderByDescending(b => b.ReleaseDate)
                                .ThenBy(b => b.Title)
                                .Take(3)
                                .Select(b => new
                                    {
                                        b.Title,
                                        b.ReleaseDate.Year
                                    })
                        });

            foreach (var category in categoriesRecentBooks)
            {
                Console.WriteLine("--{0}: {1} books", category.Category, category.BooksCount);
                foreach (var book in category.Books)
                {
                    Console.WriteLine("{0} ({1})", book.Title, book.Year);
                }
            }

            Console.WriteLine();

            // Problem 07
            var booksToAddRelatedBook = context.Books
                .Take(3)
                .ToList();
            booksToAddRelatedBook[0].RelatedBooks.Add(booksToAddRelatedBook[1]);
            booksToAddRelatedBook[1].RelatedBooks.Add(booksToAddRelatedBook[0]);
            booksToAddRelatedBook[0].RelatedBooks.Add(booksToAddRelatedBook[2]);
            booksToAddRelatedBook[2].RelatedBooks.Add(booksToAddRelatedBook[0]);

            context.SaveChanges();

            // Query the first three books
            // and get their names and their related book names
            var booksFromQuery = context.Books
                .Take(3)
                .Select(b => new
                    {
                        b.Title,
                        RelatedBooks = b.RelatedBooks.Select(r => r.Title)
                    });

            foreach (var book in booksFromQuery)
            {
                Console.WriteLine("--{0}", book.Title);
                foreach (var relatedBook in book.RelatedBooks)
                {
                    Console.WriteLine(relatedBook);
                }
            }
        }
 private static void AuthorsOrderedByBookCount(BookShopContext db)
 {
     var authors = db.Authors
         .Select(a => new
         {
             FirstName = a.FirstName,
             LastName = a.LastName,
             BookCount = a.Books.Count
         }).OrderByDescending(a => a.BookCount);
     foreach (var author in authors)
     {
         Console.WriteLine(author.FirstName + " " + author.LastName + " " + author.BookCount);
     }
 }
 private static void BooksReleasedAfter2000(BookShopContext db)
 {
     var books = db.Books
         .Where(b => b.ReleaseDate.Value.Year >= 2000)
         .Select(b => new
         {
             b.Title,
             b.ReleaseDate.Value.Year
         }).OrderBy(b => b.Year);
     foreach (var book in books)
     {
         Console.WriteLine(book.Title + " " + book.Year);
     }
 }
        private static void FilterAuthorsByBookCountAndReleaseDate(BookShopContext db)
        {
            var authors = db.Authors
                .Where(a => a.Books.Count > 0)
                .Where(a => a.Books
                    .Any(b => b.ReleaseDate.Value.Year < 1990))
                .Select(a => new
                {
                    a.FirstName,
                    Books = a.Books.Where(b => b.ReleaseDate.Value.Year < 1990).Select(b => new
                    {
                        b.Title,
                        b.ReleaseDate.Value.Year
                    })
                })
                .OrderBy(a => a.FirstName);

            foreach (var author in authors)
            {
                Console.WriteLine(author.FirstName + " --> book count:  " + author.Books.Count());
                var books = author.Books;
                Console.WriteLine("Books:");
                foreach (var book in books)
                {
                    Console.WriteLine("         " + book.Title + " " + book.Year);
                }
            }
        }
        static void Main()
        {
            
            var db = new BookShopContext();
            var bookCount = db.Books.Count();

            //problem 1
            //var booksAfter2000 = db.Books.Where(b => b.releaseDate >= new DateTime(2000, 1, 1)).Select(b => b.Title).ToList();
            //foreach (var book in booksAfter2000)
            //{
            //    Console.WriteLine(book);
            //}

            //problem 2
            //var allAuthorWithReleasBookAfter1990 =
            //    db.Authors.Where(a => a.Books.Any(b => b.releaseDate <= new DateTime(1990, 1, 1))).Select(a => new
            //    {
            //        a.FirstName,
            //        a.LastName
            //    }).ToList();
            //foreach (var author in allAuthorWithReleasBookAfter1990)
            //{
            //    Console.WriteLine(author);
            //}

            //problem 3
            //var authorsOrderedByBooks = db.Authors.OrderByDescending(a => a.Books.Count).Select(a => new
            //{
            //    FirstName = a.FirstName,
            //    LastName = a.LastName,
            //    BooksCount = a.Books.Count
            //});

            //foreach (var author in authorsOrderedByBooks)
            //{
            //    Console.WriteLine("{0} {1} : books count: {2}", author.FirstName, author.LastName, author.BooksCount);
            //}

            //problem 4
            //var allBooksFromGeorgePowell = db.Books.Where(
            //    b => b.Author.FirstName + " " + b.Author.LastName == "George Powell")
            //    .OrderByDescending(b => b.releaseDate)
            //    .ThenBy(b => b.Title)
            //    .Select(b => new
            //    {
            //        Title = b.Title,
            //        ReleaseData = b.releaseDate,
            //        Copies = b.copies
            //    }).ToList();

            //foreach (var book in allBooksFromGeorgePowell)
            //{
            //    Console.WriteLine("Book: {0}, {1}, {2} copies", book.Title, book.ReleaseData, book.Copies);
            //}

            
            
            //add books in realted books many to many
            //var books = db.Books.Take(3).ToList();
            //books[0].RelatedBooks.Add(books[1]);
            //books[1].RelatedBooks.Add(books[0]);
            //books[0].RelatedBooks.Add(books[2]);
            //books[2].RelatedBooks.Add(books[0]);

            //db.SaveChanges();

            var books = db.Books.Where(b => b.RelatedBooks.Any());

            foreach (var book in books)
            {
                Console.WriteLine("--" + book.Title);
                foreach (var relatedBooks in book.RelatedBooks)
                {
                    Console.WriteLine(relatedBooks.Title);
                }
            }
        }
        static void Main()
        {
            var context = new BookShopContext();

            //  1.	Get all books after the year 2000. Select only their titles.

            /*
            var booksAfterTheYear2000 = context.Books
                .Where(b => b.RelesaeDate.Year == 2000)
                .Select(b => b.Title)
                .ToList();

            foreach (var book in booksAfterTheYear2000)
            {
                Console.WriteLine("Book title: " + book);
            }
            */

            //  2.	Get all authors with at least one book with release date before 1990. Select their first name and last name.

            /*
            var authorsWithAtLeastOneBookWithReleaseDateBefore1990 = context.Authors
                .Where(a => a.Books.Where(b => b.RelesaeDate.Year < 1990).Count() > 0)
                .Select(a => new { a.FirstName, a.LastName})
                .ToList();

            foreach (var author in authorsWithAtLeastOneBookWithReleaseDateBefore1990)
            {
                Console.WriteLine(author.FirstName + " " + author.LastName);
            }
            */

            //  3.	Get all authors, ordered by the number of their books (descending).
            //  Select their first name, last name and book count.

            /*
            var authorsOrderedByTheNumberOfTheirBooks = context.Authors
                .Select(a => new { a.Books.Count, a.FirstName, a.LastName })
                .OrderByDescending(a => a.Count)
                .ToList();

            foreach (var author in authorsOrderedByTheNumberOfTheirBooks)
            {
                Console.WriteLine("Books count: " + author.Count + ", Author name: " + author.FirstName + " " + author.LastName);
            }
            */

            //  4.	Get all books from author George Powell, ordered by their release date (descending), then by book title (ascending).
            //  Select the book's title, release date and copies.

            /*
            var getBooksGeorgePowell = context.Books
                .Where(b => (b.Author.FirstName + " " + b.Author.LastName) == "George Powell")
                .OrderByDescending(b => b.RelesaeDate)
                .ThenBy(b => b.Title)
                .Select(b => new { b.Title, b.RelesaeDate, b.Copies })
                .ToList();

            foreach (var book in getBooksGeorgePowell)
            {
                Console.WriteLine(
                    "Title: " + book.Title +
                    ", Release Date: " + book.RelesaeDate.ToString() +
                    ", Copies: " + book.Copies);
            }
            */

            //  5.	Get the most recent books by categories. The categories should be ordered by total book count.
            //   Only take the top 3 most recent books from each category - ordered by date (descending), then by title (ascending).
            // Select the category name, total book count and for each book - its title and release date.

            /*
            var mostRecentBooksByCategory = context.Categories
                .OrderByDescending(c => c.Books.Count)
                .Select(c => new { c.Name, c.Books })
                .ToList();

            foreach (var category in mostRecentBooksByCategory)
            {
                var topBooks = category.Books.OrderByDescending(b => b.RelesaeDate).Take(3);
                Console.WriteLine("--" + category.Name + ": " + category.Books.Count);
                foreach (var book in topBooks)
                {
                    Console.WriteLine(book.Title + " (" + book.RelesaeDate.Year + ")");
                }
            }
            */

            //var books = context.Books
            //    .Take(3)
            //    .ToList();

            //books[0].RelatedBooks.Add(books[1]);
            //books[1].RelatedBooks.Add(books[0]);
            //books[0].RelatedBooks.Add(books[2]);
            //books[2].RelatedBooks.Add(books[0]);

            //context.SaveChanges();

            var booksFromQuery = context.Books
                .Take(3)
                .ToList();

            foreach (var book in booksFromQuery)
            {
                Console.WriteLine("--{0}", book.Title);
                foreach (var relatedBook in book.RelatedBooks)
                {
                    Console.WriteLine(relatedBook.Title);
                }
            }
        }
示例#22
0
        static void Main()
        {
            //set and seed database
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<BookShopContext, Configuration>());

            var context = new BookShopContext();

            var bookCount = context.Books.Count();
            Console.WriteLine(bookCount);

            //LINQ queries

            //1.Get all books after the year 2000. Select only their titles.
            var booksByYear = context.Books
                .Where(b => b.ReleaseDate.Year > 2000)
                .Select(b => b.Title);
            foreach (var book in booksByYear)
            {
                Console.WriteLine(book);
            }
            Console.WriteLine();

            //2.Get all authors with at least one book with release date before 1990. Select their first name and last name.
            var authorsWithBooks = context.Authors
                .Where(a => a.Books.Any(b => b.ReleaseDate.Year < 1990))
                .Select(a => new
                {
                    a.FirstName,
                    a.LastName
                });
            foreach (var author in authorsWithBooks)
            {
                Console.WriteLine(author);
            }
            Console.WriteLine();

            //3.Get all authors, ordered by the number of their books (descending). Select their first name, last name and book count.
            var authors = context.Authors
                .OrderByDescending(a => a.Books.Count)
                .Select(a => new
                {
                    a.FirstName,
                    a.LastName,
                    NumberOfBooks = a.Books.Count
                });
            foreach (var author in authors)
            {
                Console.WriteLine(author);
            }
            Console.WriteLine();

            //4.Get all books from author George Powell, ordered by their release date (descending), then by book title (ascending).
            //Select the book's title, release date and copies.
            var booksByAuthor = context.Books
                .OrderByDescending(b => b.ReleaseDate)
                .ThenBy(b => b.Title)
                .Where(b => b.Author.FirstName == "George" && b.Author.LastName == "Powell")
                .Select(b => new
                {
                    b.Title,
                    b.ReleaseDate,
                    b.Copies
                });
            foreach (var book in booksByAuthor)
            {
                Console.WriteLine(book);
            }
            Console.WriteLine();

            //5.Get the most recent books by categories. The categories should be ordered by total book count.
            //Only take the top 3 most recent books from each category - ordered by date (descending), then by title (ascending).
            //Select the category name, total book count and for each book - its title and release date.
            var booksByCategories = context.Categories
                .OrderByDescending(c => c.Books.Count)
                .Select(c => new
                {
                    c.Name,
                    NumberOfBooks = c.Books.Count,
                    Books = c.Books.Select(b => new
                    {
                        b.Title,
                        b.ReleaseDate
                    })
                    .OrderByDescending(b => b.ReleaseDate)
                    .ThenBy(b => b.Title)
                    .Take(3)
                });
            foreach (var category in booksByCategories)
            {
                Console.WriteLine("--" + category.Name + ": " + category.NumberOfBooks + " books");
                var books = category.Books;
                foreach (var book in books)
                {
                    Console.WriteLine(book);
                }
            }
            Console.WriteLine();

            //Populate table RelatedBooks

            //Query 3 books from the database and set them as related
            var selectedBooks = context.Books
                .Take(3)
                .ToList();

            selectedBooks[0].RelatedBooks.Add(selectedBooks[1]);
            selectedBooks[1].RelatedBooks.Add(selectedBooks[0]);
            selectedBooks[0].RelatedBooks.Add(selectedBooks[2]);
            selectedBooks[2].RelatedBooks.Add(selectedBooks[0]);

            context.SaveChanges();

            //query the first 3 books
            var booksFromQuery = context.Books
                .Take(3)
                .Select(b => new
                {
                    b.Title,
                    RelatedBooks = b.RelatedBooks.Select(rb => rb.Title)
                });
            foreach (var book in booksFromQuery)
            {
                Console.WriteLine("--{0}", book.Title);
                foreach (var relatedBook in book.RelatedBooks)
                {
                    Console.WriteLine(relatedBook);
                }
            }
        }
        public static void Main()
        {
            var context = new BookShopContext();
            var bookCount = context.Books.Count();

            //// Problem 06 - 01
            //var books = context.Books
            //    .Where(b => b.ReleaseDate.Year > 2000)
            //    .Select(b => b.Title);

            //foreach (var book in books)
            //{
            //    Console.WriteLine(book);
            //}

            //Console.WriteLine();

            //// Problem 06 - 02
            //var authors = context.Authors
            //    .Where(a => a.Books.Any(b => b.ReleaseDate.Year < 1990))
            //    .Select(a => new
            //        {
            //            a.FirstName,
            //            a.LastName
            //        });

            //foreach (var author in authors)
            //{
            //    Console.WriteLine(author.FirstName + " " + author.LastName);
            //}

            //Console.WriteLine();

            //// Problem 06 - 03
            //var authorsByBooksCount = context.Authors
            //    .OrderByDescending(a => a.Books.Count)
            //    .Select(a => new
            //        {
            //            a.FirstName,
            //            a.LastName,
            //            a.Books.Count
            //        });

            //foreach (var author in authorsByBooksCount)
            //{
            //    Console.WriteLine(author.FirstName + " " + author.LastName + ", " + author.Count);
            //}

            //Console.WriteLine();

            //// Problem 06 - 04
            //var booksByGivenAuthor =
            //    context.Books.Where(b => b.Author.FirstName == "George" && b.Author.LastName == "Powell")
            //        .OrderByDescending(b => b.ReleaseDate)
            //        .ThenBy(b => b.Title)
            //        .Select(b => new
            //            {
            //                b.Title,
            //                b.ReleaseDate,
            //                b.Copies
            //            });

            //foreach (var book in booksByGivenAuthor)
            //{
            //    Console.WriteLine(book.Title + ", " + book.ReleaseDate + ", " + book.Copies);
            //}

            //Console.WriteLine();

            //// Problem 06 - 05
            //var categoriesRecentBooks =
            //    context.Categories.OrderByDescending(c => c.Books.Count)
            //        .Select(c => new
            //            {
            //                Category = c.Name,
            //                BooksCount = c.Books.Count,
            //                Books = c.Books
            //                    .OrderByDescending(b => b.ReleaseDate)
            //                    .ThenBy(b => b.Title)
            //                    .Take(3)
            //                    .Select(b => new
            //                        {
            //                            b.Title,
            //                            b.ReleaseDate.Year
            //                        })
            //            });

            //foreach (var category in categoriesRecentBooks)
            //{
            //    Console.WriteLine("--{0}: {1} books", category.Category, category.BooksCount);
            //    foreach (var book in category.Books)
            //    {
            //        Console.WriteLine("{0} ({1})", book.Title, book.Year);
            //    }
            //}

            //Console.WriteLine();

            //// Problem 07
            //var booksToAddRelatedBook = context.Books
            //    .Take(3)
            //    .ToList();
            //booksToAddRelatedBook[0].RelatedBooks.Add(booksToAddRelatedBook[1]);
            //booksToAddRelatedBook[1].RelatedBooks.Add(booksToAddRelatedBook[0]);
            //booksToAddRelatedBook[0].RelatedBooks.Add(booksToAddRelatedBook[2]);
            //booksToAddRelatedBook[2].RelatedBooks.Add(booksToAddRelatedBook[0]);

            //context.SaveChanges();

            //// Query the first three books
            //// and get their names and their related book names
            //var booksFromQuery = context.Books
            //    .Take(3)
            //    .Select(b => new
            //        {
            //            b.Title,
            //            RelatedBooks = b.RelatedBooks.Select(r => r.Title)            
            //        });

            //foreach (var book in booksFromQuery)
            //{
            //    Console.WriteLine("--{0}", book.Title);
            //    foreach (var relatedBook in book.RelatedBooks)
            //    {
            //        Console.WriteLine(relatedBook);
            //    }
            //}
        }