Stan0033

Untitled

Jul 20th, 2022 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1.   public static string GetBooksByCategory(BookShopContext context, string input)
  2.         {
  3.             //Return in a single string the titles of books by a given list of categories.
  4.             //Order by title alphabetically.
  5.             string[] categories = input.Split(' ');
  6.             //------------------------------------------------------------------
  7.             StringBuilder output = new StringBuilder();
  8.             Category[] bCaterogies = new Category[categories.Length];
  9.             for (int i = 0; i < categories.Length; i++) { bCaterogies[i].Name = categories[i]; }
  10.             BookCategory[] addedCategories = new BookCategory[bCaterogies.Length];
  11.             for (int i = 0; i < bCaterogies.Length; i++) { addedCategories[i].Category = bCaterogies[i]; } //add the string categories in BookCategory array to compare
  12.              //------------------------------------------------------------------
  13.             string[] bookTitles = context
  14.                 .Books
  15.  
  16.                  .Where(book => book.BookCategories.ToArray() == addedCategories )
  17.                  
  18.                 .Select(book => book.Title)
  19.                 .OrderBy(book => book)
  20.                 .ToArray();
  21.  
  22.                 foreach(string title in bookTitles)
  23.             {
  24.                 output.AppendLine(title);
  25.             }
  26.  
  27.             return output.ToString().TrimEnd();
  28.         }
  29.  
  30.  //problem 6
  31.         public static string GetBooksByCategory(BookShopContext context, string input)
  32.         {
  33.             StringBuilder output = new StringBuilder();
  34.             //Return in a single string the titles of books by a given list of categories.
  35.             //Order by title alphabetically.
  36.             string[] categories = input.Split(' ');
  37.            
  38.             string[] bookTitles = context
  39.                 .Books
  40.  
  41.                  .Where(book => categories.Contains(book.BookCategories.ToString()))
  42.                  
  43.                 .Select(book => book.Title)
  44.                 .OrderBy(book => book)
  45.                 .ToArray();
  46.  
  47.                 foreach(string title in bookTitles)
  48.             {
  49.                 output.AppendLine(title);
  50.             }
  51.  
  52.             return output.ToString().TrimEnd();
  53.         }
Add Comment
Please, Sign In to add comment