Beispiel #1
0
        internal static List <CompleteBook> Search(string search, bool chkSwitch)
        {
            List <CompleteBook> books = new List <CompleteBook>();

            //spliting the search string, approximating everything after first space as surname
            //in order to search for eg. "Anne Rice"
            string[] names = search.ToString().Trim().Split(new char[] { ' ' }, 2);
            string   name  = names[0];
            string   surname;

            //check if there are at least two words in search string
            if (names.Length > 1)
            {
                surname = names[1];
            }
            else
            {
                surname = name;
            }

            using (var dbLibrary = new MyLibraryContext())
            {
                var booksLib = (from b in dbLibrary.Books
                                join a in dbLibrary.Authors on b.AuthorId equals a.AuthorId
                                join g in dbLibrary.Genres on b.GenreId equals g.GenreId
                                where (chkSwitch ? (a.Name.Contains(name) || a.Surname.Contains(surname)) : b.Title.Contains(search))
                                orderby b.Title
                                select new { b.BookId, b.Title, a.Name, a.Surname, g.GenreId, g.GenreName, b.ImageUrl }).ToList();

                foreach (var book in booksLib)
                {
                    CompleteBook newBook = new CompleteBook();
                    newBook.BookId        = book.BookId;
                    newBook.Title         = book.Title;
                    newBook.AuthorName    = book.Name;
                    newBook.AuthorSurname = book.Surname;
                    newBook.GenreId       = book.GenreId;
                    newBook.Genre         = book.GenreName;
                    newBook.CoverImageUrl = book.ImageUrl;
                    books.Add(newBook);
                }
            }

            return(books);
        }
Beispiel #2
0
        public static List <CompleteBook> GetCompleteBooks(int GenreId)
        {
            MyLibraryContext dbLibrary = new MyLibraryContext();

            var books2 = (from b in dbLibrary.Books
                          join a in dbLibrary.Authors on b.AuthorId equals a.AuthorId
                          join g in dbLibrary.Genres on b.GenreId equals g.GenreId
                          orderby b.Title
                          select new { b.BookId, b.Title, a.Name, a.Surname, g.GenreId, g.GenreName, b.ImageUrl }).ToList();

            if (GenreId != -1)
            {
                books2 = (from b in dbLibrary.Books
                          join a in dbLibrary.Authors on b.AuthorId equals a.AuthorId
                          join g in dbLibrary.Genres on b.GenreId equals g.GenreId
                          where g.GenreId == GenreId
                          orderby b.Title
                          select new { b.BookId, b.Title, a.Name, a.Surname, g.GenreId, g.GenreName, b.ImageUrl }).ToList();
            }


            //I made a class with all the elements of one book, gather from 3 tables: Books, Authors and Genres
            List <CompleteBook> books = new List <CompleteBook>();

            foreach (var book in books2)
            {
                var modelBook = new CompleteBook();
                modelBook.BookId        = book.BookId;
                modelBook.Title         = book.Title;
                modelBook.AuthorName    = book.Name;
                modelBook.AuthorSurname = book.Surname;
                modelBook.GenreId       = book.GenreId;
                modelBook.Genre         = book.GenreName;
                modelBook.CoverImageUrl = book.ImageUrl;

                books.Add(modelBook);
            }

            return(books);
        }