Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetBooksByCategory(BookShopContext context, string input)
- {
- //Return in a single string the titles of books by a given list of categories.
- //Order by title alphabetically.
- string[] categories = input.Split(' ');
- //------------------------------------------------------------------
- StringBuilder output = new StringBuilder();
- Category[] bCaterogies = new Category[categories.Length];
- for (int i = 0; i < categories.Length; i++) { bCaterogies[i].Name = categories[i]; }
- BookCategory[] addedCategories = new BookCategory[bCaterogies.Length];
- for (int i = 0; i < bCaterogies.Length; i++) { addedCategories[i].Category = bCaterogies[i]; } //add the string categories in BookCategory array to compare
- //------------------------------------------------------------------
- string[] bookTitles = context
- .Books
- .Where(book => book.BookCategories.ToArray() == addedCategories )
- .Select(book => book.Title)
- .OrderBy(book => book)
- .ToArray();
- foreach(string title in bookTitles)
- {
- output.AppendLine(title);
- }
- return output.ToString().TrimEnd();
- }
- //problem 6
- public static string GetBooksByCategory(BookShopContext context, string input)
- {
- StringBuilder output = new StringBuilder();
- //Return in a single string the titles of books by a given list of categories.
- //Order by title alphabetically.
- string[] categories = input.Split(' ');
- string[] bookTitles = context
- .Books
- .Where(book => categories.Contains(book.BookCategories.ToString()))
- .Select(book => book.Title)
- .OrderBy(book => book)
- .ToArray();
- foreach(string title in bookTitles)
- {
- output.AppendLine(title);
- }
- return output.ToString().TrimEnd();
- }
Add Comment
Please, Sign In to add comment