Bookstore.DataAccessLayer.BooksDAO.SearchBooks C# (CSharp) Метод

SearchBooks() публичный Метод

public SearchBooks ( string title, string author, string isbn ) : IEnumerable
title string
author string
isbn string
Результат IEnumerable
        public IEnumerable<FoundBookTransferObject> SearchBooks(string title, string author, string isbn)
        {
            BookstoreEntities context = new BookstoreEntities();
            var books = context.Books.Include("Reviews").AsQueryable<Book>();
            if (title != null)
            {
                books = books.Where(b => b.Title.ToLower() == title);
            }

            if (author != null)
            {
                books = books.Where(b => b.Authors.Any(a => a.Name.ToLower() == author));
            }

            if (isbn != null)
            {
                books = books.Where(b => b.ISBN == isbn);
            }

            books = books.OrderBy(b => b.Title);
            IList<FoundBookTransferObject> foundBooksInfos = new List<FoundBookTransferObject>();
            foreach (Book book in books)
            {
                foundBooksInfos.Add(new FoundBookTransferObject()
                    {
                        BookTitle = book.Title,
                        ReviewsCount = book.Reviews.Count
                    });
            }

            return foundBooksInfos;
        }
    }

Usage Example

        static void Main()
        {
            BooksDAO booksDao = new BooksDAO();

            XmlDocument queryDocument = new XmlDocument();
            queryDocument.Load("../../simple-query.xml");

            string title = GetTitle(queryDocument);
            string author = GetAuthorName(queryDocument);
            string isbn = GetIsbn(queryDocument);

            IEnumerable<FoundBookTransferObject> foundBooksInfos = 
                booksDao.SearchBooks(title, author, isbn);
            int booksInfosCount = foundBooksInfos.Count();
            if (booksInfosCount != 0)
            {
                Console.WriteLine("{0} books found:", booksInfosCount);
                foreach (FoundBookTransferObject bookInfo in foundBooksInfos)
                {
                    int reviewsCount = bookInfo.ReviewsCount;
                    Console.WriteLine("{0} --> {1} reviews",
                        bookInfo.BookTitle, 
                        reviewsCount != 0 ? bookInfo.ReviewsCount.ToString() : "no");
                }
            }
            else
            {
                Console.WriteLine("Nothing found");
            }
        }