public List <PopularBook> GetMostPopularBooksByAgeGroup() { var allPopularBooks = new List <PopularBook>(); var allBooksBorrowedByAgeGroup = GetArrayWithListsOfAllBooksInEachAgeGroup(); int yearGroupLowestAge = 0; int yearGroupMaxAge = 9; for (int i = 0; i < 10; i++) { var mostPopularBookInAgeGroup = (from b in allBooksBorrowedByAgeGroup[i] group b by b into grp orderby grp.Count() descending select grp.Key).FirstOrDefault(); if (mostPopularBookInAgeGroup != null) { var popularBook = new PopularBook(); popularBook.Book = mostPopularBookInAgeGroup; popularBook.AgeGroupTenYearsFromThisAge = yearGroupLowestAge; allPopularBooks.Add(popularBook); } yearGroupLowestAge += 10; yearGroupMaxAge += 10; } return(allPopularBooks); }
static void Main(string[] args) { PopularBook[] arr; arr = new PopularBook[5]; for(int i=0;i<5;i++){ Console.WriteLine("Enter title: "); string a =Console.ReadLine(); Console.WriteLine("Enter Auther: "); string b =Console.ReadLine(); arr[i] =new PopularBook(a,b); arr[i].display(); } }