コード例 #1
0
        static void Main()
        {
            using (var context = new BookShopContext())
            {
                var authors = context.Authors.Count();

                Console.WriteLine(authors);
            }
        }
コード例 #2
0
ファイル: Index.cshtml.cs プロジェクト: YWFriend/BookShop
 public IndexModel(BookShop.Data.BookShopContext context)
 {
     _context = context;
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: coasterka/SoftUniBackEnd
 static void Main(string[] args)
 {
     var context = new BookShopContext();
     var auth = context.Authors.Count();
     Console.WriteLine(auth);
 }
コード例 #4
0
 public DetailsModel(BookShop.Data.BookShopContext context)
 {
     _context = context;
 }
コード例 #5
0
 public CreateModel(BookShop.Data.BookShopContext context)
 {
     _context = context;
 }
コード例 #6
0
 public EditModel(BookShop.Data.BookShopContext context)
 {
     _context = context;
 }
コード例 #7
0
        private static void Main()
        {
            var context = new BookShopContext();

            var BooksCount = context.Books.Count();

            //Problem 6.1

            var Books = context.Books
                .Where(b => b.ReleaseDate > new DateTime(2000, 1, 1))
                .Select(b => new
                {
                    b.Title
                });

            foreach (var book in Books)
            {
                Console.WriteLine("{0}", book.Title);
            }

            //Problem 6.2

            var Authors = context.Authors
                .Where(a => (context.Books.Where(b => b.AuthorId == a.Id && b.ReleaseDate < new DateTime(1990, 1, 1)))
                    .Any())
                .Select(a => new
                {
                    a.FirstName,
                    a.LastName
                });

            foreach (var author in Authors)
            {
                Console.WriteLine("{0} {1}",
                    author.FirstName,
                    author.LastName);
            }

            //Problem 6.3

            var AuthorsByNum = context.Authors.Select(a => new
            {
                a.FirstName,
                a.LastName,
                BookCount = context.Books.Count(b => b.AuthorId == a.Id)
            }).OrderByDescending(a => a.BookCount);

            foreach (var author in AuthorsByNum)
            {
                Console.WriteLine("{0} {1} {2}",
                    author.FirstName,
                    author.LastName,
                    author.BookCount);
            }

            //Problem 6.4

            var BooksByGeorge = 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 BooksByGeorge)
            {
                Console.WriteLine("{0} {1} {2}",
                    Book.Title,
                    Book.ReleaseDate,
                    Book.Copies);
            }

            //Problem 6.5

            var TopBooks = context.Categories
                .OrderByDescending(c => c.Books.Count)
                .Select(c => new
                {
                    c.Name,
                    TotalBooks = c.Books.Count,
                    Books = context.Books
                        .Where(b => b.Categories.Any(a => a.Name == c.Name))
                        .OrderByDescending(b => b.ReleaseDate)
                        .ThenBy(b => b.Title)
                        .Take(3)
                        .Select(b => new
                        {
                            b.Title,
                            b.ReleaseDate
                        })
                });

            foreach (var topBook in TopBooks)
            {
                Console.WriteLine("--{0}:  {1} books",
                    topBook.Name,
                    topBook.TotalBooks);
                foreach (var book in topBook.Books)
                {
                    Console.WriteLine("{0} ({1})",
                        book.Title,
                        book.ReleaseDate.Value.Year
                        );
                }
            }

            //Problem 7

            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();

            foreach (var book in books)
            {
                Console.WriteLine("--{0}", book.Title);
                foreach (var relatedBook in book.RelatedBooks)
                {
                    Console.WriteLine(relatedBook.Title);
                }
            }
        }
コード例 #8
0
 public BaseApiController(BookShopContext data)
 {
     this.Data = data;
 }
コード例 #9
0
        static void Main()
        {
            var context = new BookShopContext();
            //var count = context.Categories.Count();
            //Console.WriteLine(count);

            using (var reader = new StreamReader("authors.txt"))
            {
                var line = reader.ReadLine();
                line = reader.ReadLine();
                while (line != null)
                {
                    var data = line.Split(new[] { ' ' }, 2);
                    string firstName = data[0];
                    string lastName = data[1];
                    context.Authors.Add(new Author()
                    {
                        FirstName = firstName,
                        LastName = lastName
                    });
                    line = reader.ReadLine();
                    context.SaveChanges();
                }
            }

            using (var categoryReader = new StreamReader("categories.txt"))
            {
                var line = categoryReader.ReadLine();
                line = categoryReader.ReadLine();
                while (line != null)
                {
                    context.Categories.Add(new Category()
                    {
                        Name = line
                    });
                    line = categoryReader.ReadLine();
                    context.SaveChanges();
                }
            }


            Random rnd = new Random();
            var authors = context.Authors.ToList();
            var categories = context.Categories.ToList();
            CultureInfo provider = CultureInfo.InvariantCulture;
            using (var bookReader = new StreamReader("books.txt"))
            {
                var line = bookReader.ReadLine();
                line = bookReader.ReadLine();
                while (line != null)
                {
                    var data = line.Split(new[] { ' ' }, 6);
                    var authorIndex = rnd.Next(0, authors.Count);
                    var author = authors[authorIndex];
                    var categoryIndex = rnd.Next(0, categories.Count);
                    var category = categories[categoryIndex];
                    var edition = (EditionType)int.Parse(data[0]);
                    var releaseDate = DateTime.ParseExact(data[1], "d/m/yyyy", provider);
                    var copies = int.Parse(data[2]);
                    var price = Double.Parse(data[3]);
                    var ageRestriction = int.Parse(data[4]);
                    var title = data[5];
                    context.Books.Add(new Book()
                    {
                        Author = author,
                        Title = title,
                        Edition = edition,
                        ReleaseDate = releaseDate,
                        Copies = copies,
                        Price = price,
                        AgeRestriction = ageRestriction
                    });
                    context.SaveChanges();
                    var book = context.Books.FirstOrDefault(b => b.Title == title);
                    book.Categories.Add(category);
                    line = bookReader.ReadLine();
                }
            }
        }
コード例 #10
0
 public DeleteModel(BookShop.Data.BookShopContext context)
 {
     _context = context;
 }
コード例 #11
0
 public BooksController()
 {
     this.context = new BookShopContext();
 }