コード例 #1
0
        public ActionResult Index(string author, int?year, string genre)
        {
            using (LibraryContext db = new LibraryContext())
            {
                IQueryable <Book>     books            = db.Books;
                List <BookGridEntity> bookGridEntities = new List <BookGridEntity>();

                if (!String.IsNullOrEmpty(author))
                {
                    int id = BookService.GetAuthorIdByName(author);
                    books = books.Where(b => b.AuthorId == id);
                }

                if (year != null && year != 0)
                {
                    books = books.Where(b => b.Year == year);
                }

                if (!String.IsNullOrEmpty(genre))
                {
                    int id = BookService.GetGanreIdByName(genre);
                    books = books.Where(b => b.GenreId == id);
                }

                foreach (Book book in books.ToList())
                {
                    bookGridEntities.Add(BookService.GetGridEntity(book));
                }

                BooksListViewModel booksListView = new BooksListViewModel
                {
                    BookGridEntities = bookGridEntities,
                    Author           = author,
                    Year             = year,
                    Ganre            = genre,
                };

                return(View(booksListView));
            }
        }