private void MainForm_Load(object sender, EventArgs e) { // authors are gotten from http://www.worldswithoutend.com BooksLoader booksLoader = new BooksLoader(); authors = booksLoader.GetAllAuthors(); lbxAuthors.DataSource = authors; lbxAuthors.DisplayMember = "Name"; }
private void MainForm_Load(object sender, EventArgs e) { BooksLoader loader = new BooksLoader(); authors = loader.GetAllAuthors(); LbxAuthors.DataSource = authors; LbxAuthors.DisplayMember = "Name"; pictureBox.Location = new Point(756, 12); pictureBox.Size = new Size(874, 330); pictureBox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom; pictureBox.TabIndex = 0; pictureBox.TabStop = false; Controls.Add(pictureBox); }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); // Console.WriteLine(authors.Count()); // Console.WriteLine(authors.Count(a => a.Books.Count() >=50)); //var authorsResult = authors.Where(a => //{ // Console.WriteLine($"Checking author {a.Name}"); // return a.Books.Count() >= 50; //}); //foreach (var author in authorsResult) //{ // Console.WriteLine($"{author.Name} has {author.Books.Count()} books"); // Console.ReadLine(); //} //Console.WriteLine(authorsResult.Count()); var authorCount = authors.Count(a => a.Books.Count() >= 50); Console.WriteLine($"Authors with more than 50 books: {authorCount}"); authorCount = authors.Count(a => a.Books.Count(b => string.IsNullOrEmpty(b.Series)) >= 20); Console.WriteLine($"Authors with more than 20 non-series books: {authorCount}"); authorCount = authors.Count(a => a.Books.Count(b => b.SeriesIndex == null) >= 20); Console.WriteLine($"Authors with more than 20 non-series books: {authorCount}"); //Console.WriteLine(authors.Count(a => a.Books.Count(b => string.IsNullOrEmpty(b.Series)) >= 20)); //Console.WriteLine(authors.Count(a => a.Books.Count(b => b.SeriesIndex == null) >= 20)); var weirdBooks = from a in authors from b in a.Books where b.SeriesIndex == null && !string.IsNullOrEmpty(b.Series) select b; //foreach (var book in weirdBooks) //{ // Console.WriteLine(book); //} var weirdBooks2 = authors.SelectMany(a => a.Books.Where(b => b.SeriesIndex == null && !string.IsNullOrEmpty(b.Series))); Console.WriteLine(weirdBooks2.Count()); var allBooks = authors.SelectMany(a => a.Books); Console.WriteLine(allBooks.Count()); Console.WriteLine(allBooks.Where(b => b.Title.StartsWith("X")).First()); var allBooksWithAuthor = authors.SelectMany(a => a.Books.Select(b => new { b.ID, a.Name, b.Title })); Console.WriteLine(allBooksWithAuthor.Count()); // We KNOW we will have at least one element Console.WriteLine(allBooksWithAuthor.First(b => b.Title.StartsWith("X"))); // We DON'T KNOW if we will have at least one element Console.WriteLine(allBooksWithAuthor.FirstOrDefault(b => b.Title.StartsWith("XYZ"))); // We KNOW we will have at least one element Console.WriteLine(allBooksWithAuthor.Last(b => b.Title.StartsWith("X"))); // We DON'T KNOW if we will have at least one element Console.WriteLine(allBooksWithAuthor.LastOrDefault(b => b.Title.StartsWith("XYZ"))); // We KNOW there will be exactly one element Console.WriteLine(allBooksWithAuthor.Single(b => b.ID == 1)); // We KNOW there will be at most one element Console.WriteLine(allBooksWithAuthor.SingleOrDefault(b => b.ID == 40000)); var allAuthorsByName = authors.OrderBy(a => a.Name); Console.WriteLine(allAuthorsByName.First()); Console.WriteLine(allAuthorsByName.Last()); var allAuthorsByBookCountDesc = authors .OrderByDescending(a => a.Books.Count()) .ThenBy(a => a.Name); Console.WriteLine(allAuthorsByBookCountDesc.First()); Console.WriteLine(allAuthorsByBookCountDesc.Last()); // count of authors with the specied number of books //Console.WriteLine(authors.Count(a => a.Books.Count() == 1)); //Console.WriteLine(authors.Count(a => a.Books.Count() == 218)); var nonSeries = authors.Select(a => new { a.ID, a.Name, NonSeriesCount = a.Books.Count(b => string.IsNullOrEmpty(b.Series)) }).OrderByDescending(a => a.NonSeriesCount); Console.WriteLine(nonSeries.First()); Console.WriteLine(nonSeries.Skip(1).First()); Console.WriteLine(nonSeries.Skip(2).First()); // positions 11 to 20 var nonSeries11to20 = nonSeries.Skip(10).Take(10); PrintAuthors(nonSeries11to20); var nonSeriesBetween30and40 = nonSeries .SkipWhile(a => a.NonSeriesCount > 40) .TakeWhile(a => a.NonSeriesCount > 30); PrintAuthors(nonSeriesBetween30and40); var nonSeriesMax = authors.Select(a => new { a.ID, a.Name, NonSeriesCount = a.Books.Count(b => string.IsNullOrEmpty(b.Series)) }).Max(a => a.NonSeriesCount); Console.WriteLine(nonSeriesMax); // Grouping var booksByLetter = authors .SelectMany(a => a.Books) .GroupBy(b => b.Title.First()); Console.WriteLine(booksByLetter.Count()); var firstGrouping = booksByLetter.First(); //Console.WriteLine($"{firstGrouping.Key}: {firstGrouping.Count()}"); //PrintAuthors(firstGrouping); foreach (var group in booksByLetter) { Console.WriteLine($"{group.Key}: {group.Count()}"); } Console.WriteLine("---------------"); var letterWithMostBooks = booksByLetter.OrderByDescending(g => g.Count()).First(); Console.WriteLine($"{letterWithMostBooks.Key}: {letterWithMostBooks.Count()}"); Console.WriteLine("---------------"); //var lastGrouping = booksByLetter.Last(); //PrintAuthors(lastGrouping); var authorsByLetter = authors .GroupBy(a => a.Name.First()) .Select(g => new { g.Key, Count = g.Count() }) .OrderByDescending(g => g.Count); var letterWithMostAuthors = authorsByLetter.First(); Console.WriteLine($"{letterWithMostAuthors.Key}: {letterWithMostAuthors.Count}"); Console.WriteLine("---------------"); foreach (var group in authorsByLetter) { Console.WriteLine($"{group.Key}: {group.Count}"); } Console.WriteLine("---------------"); Console.WriteLine(authors.Single(a => a.Name.StartsWith("X"))); Console.WriteLine(authors.Any(a => a.Books.Count() == 72)); Console.WriteLine(authors.Any(a => a.Books.Count() == 222)); Console.WriteLine(authors.All(a => a.Books.Count() > 0)); Console.WriteLine(authors.All(a => a.Books.Count() > 1)); }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); // 1.What is the average number of books per autors? Console.WriteLine("1.What is the average number of books per autors?"); Console.WriteLine(); var averageNumberOfBooks = authors.Average(author => author.Books.Count()); Console.WriteLine($"Average number of books per author is {string.Format("{0:0.00}", averageNumberOfBooks)}."); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //2.Which book(s) has the longest title, and how long is it ? Console.WriteLine("2.Which book(s) has the longest title, and how long is it ?"); Console.WriteLine(); var booksTitle = authors.SelectMany(a => a.Books .Select(b => new { b.Title, TitleLength = b.Title.Count() })).OrderByDescending(t => t.TitleLength).First(); Console.WriteLine($@"The longest book title is: ""{booksTitle.Title}""with length of {booksTitle.TitleLength} chars. "); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //3. Which author has the shortest average title for a book? Console.WriteLine("3. Which author has the shortest average title for a book?"); Console.WriteLine(); var shortestAverageTitle = authors.Select(a => new { a.Name, AverageBookLength = a.Books.Average(book => book.Title.Count()) }).OrderBy(b => b.AverageBookLength); Console.WriteLine($"Author that has shortest average title for a book is {shortestAverageTitle.First().Name} with length of the title: {shortestAverageTitle.First().AverageBookLength}"); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //4. Which author has the shortest average title for a book? (Discount authors with less than three books) Console.WriteLine("4. Which author has the shortest average title for a book? (Discount authors with less than three books)"); Console.WriteLine(); var shortestAverageTitleWithDiscount = authors.Where(author => author.Books.Count() > 3) .Select(a => new { a.Name, AverageBookLength = a.Books.Average(book => book.Title.Length) }).OrderBy(book => book.AverageBookLength).First(); Console.WriteLine($"Author that has shortest average title for a book with discount auhors with less than three books is {shortestAverageTitleWithDiscount.Name} \nwith length of the title: {string.Format("{0:0.00}", shortestAverageTitleWithDiscount.AverageBookLength)}."); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //5.What series has the most books? Console.WriteLine("5.What series has the most books?"); Console.WriteLine(); var seriesWithMostBooks = authors.SelectMany(a => a.Books) .GroupBy(b => b.Series) .Select(series => new { series.Key, Count = series.Where(s => !string.IsNullOrEmpty(s.Series)).Count() }) .OrderByDescending(s => s.Count).First(); Console.WriteLine("Series with most books is: {0} with {1} books.", seriesWithMostBooks.Key, seriesWithMostBooks.Count); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //6.Which year has the most books published? Console.WriteLine("6. Which year has the most books published?"); Console.WriteLine(); var booksPerYear = authors.SelectMany(a => a.Books) .GroupBy(b => b.Year) .Select(g => new { g.Key, Count = g.Count() }) .OrderByDescending(g => g.Count); var yearWithMostPublishing = booksPerYear.First(); Console.WriteLine($"In {yearWithMostPublishing.Key}, {yearWithMostPublishing.Count} books have been published."); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //7. What is the average number of books published for years in the 21st centrury ? (Starting with 2001, not 2000) Console.WriteLine("7. What is the average number of books published for years in the 21st centrury ? (Starting with 2001, not 2000)"); Console.WriteLine(); var twentyFirstCentrury = booksPerYear .Where(year => year.Key > 2000) .OrderBy(y => y.Key) .Average(y => y.Count); Console.WriteLine("The average number of books published for years in the 21st centrury is: {0} books.", twentyFirstCentrury); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //8. Which author has the most different series? Console.WriteLine("8. Which author has the most different series?"); Console.WriteLine(); var seriesAuthors = authors.Select(a => new { a.Name, NumberOfSeries = a.Books.Where(b => !string.IsNullOrEmpty(b.Series)).GroupBy(b => b.Series).Distinct().Count() }).OrderByDescending(b => b.NumberOfSeries); Console.WriteLine("There is two authors with same number of different series:"); Console.WriteLine($"Author: {seriesAuthors.First().Name} with {seriesAuthors.First().NumberOfSeries} series."); Console.WriteLine($"Author: {seriesAuthors.Skip(1).First().Name} with {seriesAuthors.Skip(1).First().NumberOfSeries} series."); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //9. Which author has the most books written that belong to a series? Console.WriteLine("9. Which author has the most books written that belong to a series?"); Console.WriteLine(); var booksWithSeries = authors.Select(author => new { author.Name, Series = author.Books.Where(book => !string.IsNullOrEmpty(book.Series)).Count() }).OrderByDescending(b => b.Series).First(); Console.WriteLine($"Author that has the most books written that belong to a series is: {booksWithSeries.Name} with {booksWithSeries.Series} series."); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //10. Which author has the longest career? Console.WriteLine("10. Which author has the longest career?"); Console.WriteLine(); var longestCarreer = authors.Select(a => new { a.Name, YearOfCarrer = a.Books.Max(b => b.Year) - a.Books.Min(b => b.Year) }).OrderByDescending(y => y.YearOfCarrer).First(); Console.WriteLine($"The author {longestCarreer.Name} has the longest carrer of {longestCarreer.YearOfCarrer} years!"); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); //Bonus //1. What series has the most authors? /* * Console.WriteLine("1. What series has the most authors?"); * Console.WriteLine(); * * var seriesWithMostAuthors = authors.SelectMany(author => author.Books) * .Where(b => !string.IsNullOrEmpty(b.Series) && b.SeriesIndex != null) * .Select(book => new * { * book.Title, * NameOfAuthor = authors.Where(a => a.Books.Contains(book)).First().Name * }).GroupBy(t => t.Title).OrderByDescending(s => s.Count()).First(); * * * Console.WriteLine($@"The series ""{seriesWithMostAuthors.Key}"" has {seriesWithMostAuthors.Count()} authors."); * * Console.WriteLine(); * Console.WriteLine("--------------"); * Console.WriteLine(); * * //2. In Which year most authors published a book? * * Console.WriteLine("2. In Which year most authors published a book?"); * Console.WriteLine(); * * var authorsPerYear = authors.SelectMany(a => a.Books) * .GroupBy(b => b.Year) * .Select(year => year.Key) * .Select(year => new * { * Year = year, * AuthorNumber = authors.Select(a => new * { * Years = a.Books.Select(b => b.Year).Distinct() * }).Where(a => a.Years.Contains(year)) * }).OrderByDescending(y => y.AuthorNumber.Count()).First(); * * Console.WriteLine($"In {authorsPerYear.Year} total {authorsPerYear.AuthorNumber.Count()} authors have published book."); * * Console.WriteLine(); * Console.WriteLine("--------------"); * Console.WriteLine(); */ // 3. Which author has the highest average books per year? Console.WriteLine("3. Which author has the highest average books per year?"); Console.WriteLine(); var highestBooksPerYear = authors.Select(author => new { author.Name, HighestAverage = author.Books .GroupBy(b => b.Year) .Average(book => book.Count()) }).OrderByDescending(b => b.HighestAverage).First(); Console.WriteLine($"Author with the highest average books per year is: {highestBooksPerYear.Name} with {highestBooksPerYear.HighestAverage} books."); Console.WriteLine(); Console.WriteLine("--------------"); Console.WriteLine(); // 4. How long is the longest hiatus between two books for an author, and by whom? #region Books with two authors //5. books with two authors var booksWithTwoAuthors = authors .SelectMany(a => a.Books) .GroupBy(b => b.Title) .Where(g => g.Count() == 2) .Select(g => new { booktitle = g.Key }).Distinct(); Console.WriteLine(booksWithTwoAuthors); var nameOFBOOK = "So Vile a Sin"; var nesto = authors.Select(a => new { a.Name, BooksTitles = a.Books.Select(b => b.Title) }); var authorsNames = nesto.Select(n => new { Authors = nesto.Where(w => w.BooksTitles.Contains(nameOFBOOK)).Select(a => new { a.Name }) }).First().Authors; string authorInitials = ""; foreach (var item in authorsNames) { authorInitials += string.Join("", item.Name.Split(' ').Select(p => p[0])).ToString(); } Console.WriteLine(authorInitials.ToLower()); Console.ReadLine(); #endregion }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); // Console.WriteLine(authors.Count()); // Console.WriteLine(authors.Count(a => a.Books.Count() >=50)); //var authorsResult = authors.Where(a => //{ // Console.WriteLine($"Checking author {a.Name}"); // return a.Books.Count() >= 50; //}); //foreach (var author in authorsResult) //{ // Console.WriteLine($"{author.Name} has {author.Books.Count()} books"); // Console.ReadLine(); //} //Console.WriteLine(authorsResult.Count()); // var authorCount = authors.Count(a => a.Books.Count() >= 50); var authorCount = authors.Where(a => a.Books.Count() >= 70); Console.WriteLine($"Authors with more than 50 books: {authorCount}"); Console.WriteLine("--------------------------------------------------------------------------------------"); // authorCount = authors.Count(a => a.Books.Count(b => string.IsNullOrEmpty(b.Series)) >= 20); Console.WriteLine($"Authors with more than 20 non-series books: {authorCount}"); // authorCount = authors.Count(a => a.Books.Count(b => b.SeriesIndex == null) >= 20); Console.WriteLine($"Authors with more than 20 non-series books: {authorCount}"); //Console.WriteLine(authors.Count(a => a.Books.Count(b => string.IsNullOrEmpty(b.Series)) >= 20)); //Console.WriteLine(authors.Count(a => a.Books.Count(b => b.SeriesIndex == null) >= 20)); var weirdBooks = from a in authors from b in a.Books where b.SeriesIndex == null && !string.IsNullOrEmpty(b.Series) select b; //foreach (var book in weirdBooks) //{ // Console.WriteLine(book); //} var weirdBooks2 = authors.SelectMany(a => a.Books.Where(b => b.SeriesIndex == null && !string.IsNullOrEmpty(b.Series))); Console.WriteLine(weirdBooks2.Count()); var allBooks = authors.SelectMany(a => a.Books); Console.WriteLine(allBooks.Count()); Console.WriteLine(allBooks.Where(b => b.Title.StartsWith("X")).First()); var allBooksWithAuthor = authors.SelectMany(a => a.Books.Select(b => new { b.ID, a.Name, b.Title })); Console.WriteLine(allBooksWithAuthor.Count()); // We KNOW we will have at least one element Console.WriteLine(allBooksWithAuthor.First(b => b.Title.StartsWith("X"))); // We DON'T KNOW if we will have at least one element Console.WriteLine(allBooksWithAuthor.FirstOrDefault(b => b.Title.StartsWith("XYZ"))); // We KNOW we will have at least one element Console.WriteLine(allBooksWithAuthor.Last(b => b.Title.StartsWith("X"))); // We DON'T KNOW if we will have at least one element Console.WriteLine(allBooksWithAuthor.LastOrDefault(b => b.Title.StartsWith("XYZ"))); // We KNOW there will be exactly one element Console.WriteLine(allBooksWithAuthor.Single(b => b.ID == 1)); // We KNOW there will be at most one element Console.WriteLine(allBooksWithAuthor.SingleOrDefault(b => b.ID == 40000)); var allAuthorsByName = authors.OrderBy(a => a.Name); Console.WriteLine(allAuthorsByName.First()); Console.WriteLine(allAuthorsByName.Last()); var allAuthorsByBookCountDesc = authors .OrderByDescending(a => a.Books.Count()) .ThenBy(a => a.Name); Console.WriteLine(allAuthorsByBookCountDesc.First()); Console.WriteLine(allAuthorsByBookCountDesc.Last()); // count of authors with the specied number of books //Console.WriteLine(authors.Count(a => a.Books.Count() == 1)); //Console.WriteLine(authors.Count(a => a.Books.Count() == 218)); var nonSeries = authors.Select(a => new { a.ID, a.Name, NonSeriesCount = a.Books.Count(b => string.IsNullOrEmpty(b.Series)) }).OrderByDescending(a => a.NonSeriesCount); Console.WriteLine(nonSeries.First()); Console.WriteLine(nonSeries.Skip(1).First()); Console.WriteLine(nonSeries.Skip(2).First()); // positions 11 to 20 var nonSeries11to20 = nonSeries.Skip(10).Take(10); PrintAuthors(nonSeries11to20); var nonSeriesBetween30and40 = nonSeries .SkipWhile(a => a.NonSeriesCount > 40) .TakeWhile(a => a.NonSeriesCount > 30); PrintAuthors(nonSeriesBetween30and40); var nonSeriesMax = authors.Select(a => new { a.ID, a.Name, NonSeriesCount = a.Books.Count(b => string.IsNullOrEmpty(b.Series)) }).Max(a => a.NonSeriesCount); Console.WriteLine(nonSeriesMax); // Grouping var booksByLetter = authors .SelectMany(a => a.Books) .GroupBy(b => b.Title.First()); Console.WriteLine(booksByLetter.Count()); var firstGrouping = booksByLetter.First(); //Console.WriteLine($"{firstGrouping.Key}: {firstGrouping.Count()}"); //PrintAuthors(firstGrouping); foreach (var group in booksByLetter) { Console.WriteLine($"{group.Key}: {group.Count()}"); } Console.WriteLine("---------------"); var letterWithMostBooks = booksByLetter.OrderByDescending(g => g.Count()).First(); Console.WriteLine($"{letterWithMostBooks.Key}: {letterWithMostBooks.Count()}"); Console.WriteLine("---------------"); //var lastGrouping = booksByLetter.Last(); //PrintAuthors(lastGrouping); var authorsByLetter = authors .GroupBy(a => a.Name.First()) .Select(g => new { g.Key, Count = g.Count() }) .OrderByDescending(g => g.Count); var letterWithMostAuthors = authorsByLetter.First(); Console.WriteLine($"{letterWithMostAuthors.Key}: {letterWithMostAuthors.Count}"); Console.WriteLine("---------------"); foreach (var group in authorsByLetter) { Console.WriteLine($"{group.Key}: {group.Count}"); } Console.WriteLine("---------------"); Console.WriteLine(authors.Single(a => a.Name.StartsWith("X"))); Console.WriteLine(authors.Any(a => a.Books.Count() == 72)); Console.WriteLine(authors.Any(a => a.Books.Count() == 222)); Console.WriteLine(authors.All(a => a.Books.Count() > 0)); Console.WriteLine(authors.All(a => a.Books.Count() > 1)); Console.WriteLine("-----------------HOMEWORK--------------------------"); //1 // What is the average number of books per autors? var numOfBooksPerAuthors = (authors.SelectMany(a => a.Books).Count()) / (authors.Select(a => a.Books).Count()); Console.WriteLine(numOfBooksPerAuthors); // 2 //Which book(s) has the longest title, and how long is it? var longestTitle = allBooks.OrderByDescending(t => t.Title.Length).First(); var longestTitleNum = allBooks.Max(t => t.Title.Length); Console.WriteLine($"Longest title is {longestTitle} and has {longestTitleNum} letters"); // 3 // Which author has the shortest average title for a book? var shortestAvgTitle = authors.Select(a => new { a.Name, shortesAvg = a.Books.Average(b => b.Title.Length) }).OrderBy(a => a.shortesAvg).First(); Console.WriteLine(shortestAvgTitle); //4 // Which author has the shortest average title for a book? (Discount authors with less than three books) var lessThan3 = authors.Where(a => a.Books.Count() > 3) .Select(a => new { a.Name, shortesAvg = a.Books.Average(b => b.Title.Length) }) .OrderBy(a => a.shortesAvg).First(); Console.WriteLine(lessThan3); // 5 // What series has the most books? var allSeries = allBooks.Where(s => !string.IsNullOrEmpty(s.Series)).GroupBy(s => s.Series).OrderByDescending(s => s.Count()).First(); Console.WriteLine($"{allSeries.Key} is series with {allSeries.Count()} books"); // 6 // Which year has the most books published? var booksPerYer = allBooks.GroupBy(y => y.Year) .Select(b => new { b.Key, Count = b.Count() }) .OrderByDescending(b => b.Count); Console.WriteLine($"The most books are published in: {booksPerYer.First()}"); // 7 // What is the average number of books published for years in the 21st centrury ? (Starting with 2001, not 2000) var avgCentury21 = authors.SelectMany(a => a.Books).Where(b => b.Year > 2000).GroupBy(b => b.Year).Average(c => c.Count()); Console.WriteLine($"The average number of books published in 21st century is {avgCentury21}"); // Which author has the most different series? var authorWithDiffSeries = authors.Select(a => new { a.Name, seriesNumber = a.Books.GroupBy(b => b.Series).Count() }) .OrderByDescending(b => b.seriesNumber).ThenBy(n => n.Name).ToList(); Console.WriteLine(authorWithDiffSeries.First().Name); // Which author has the most books written that belong to a series? var authorWithmostSeries = authors.Select(a => new { a.Name, NumOfSeries = a.Books.Where(b => !string.IsNullOrEmpty(b.Series)).Count() }) .OrderByDescending(a => a.NumOfSeries) .First(); Console.WriteLine($"Author with most series is: {authorWithmostSeries.Name} ({authorWithmostSeries.NumOfSeries})"); // Which author has the longest career? var longestCarrier = authors.Select(a => new { a.Name, YearOfCarrer = a.Books.Select(b => b.Year).Max() - a.Books.Select(b => b.Year).Min() }) .OrderByDescending(y => y.YearOfCarrer) .First(); Console.WriteLine($"{longestCarrier.Name} has the longest career of {longestCarrier.YearOfCarrer}"); }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); /*Regular: * 1.What is the average number of books per autors? * 2.Which book(s) has the longest title, and how long is it? * 3.Which author has the shortest average title for a book? * 4.Which author has the shortest average title for a book? (Discount authors with less than three books) * 5.What series has the most books? * 6.Which year has the most books published? * 7.What is the average number of books published for years in the 21st centrury? (Starting with 2001, not 2000) * 8.Which author has the most different series? * 9.Which author has the most books written that belong to a series? * 10.Which author has the longest career? */ Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***1.The average number of books per autors:"); var averageBook = authors.Select(author => new { author.ID, author.Name, AverageBook = authors.Average(a => a.Books.Count()) }); Console.WriteLine(averageBook.First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***2.Book(s) with the longest title:"); var allBooks = authors .SelectMany(author => author.Books) .OrderByDescending(book => book.Title.Length) .ThenBy(book => book.ID); var first = allBooks.First(); Console.WriteLine($" ID:{first.ID} => Title:{first.Title} => Length:{first.Title.Count()}"); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***3.Author who has the shortest average title for a book:"); var shortestTitle = authors.Select(author => new { author.ID, author.Name, ShortTitle = author.Books.Average(book => book.Title.Length) }).OrderBy(book => book.ShortTitle).ToList(); Console.WriteLine(shortestTitle.First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***4.Author who has the shortest average title for a book:\n " "(Discount authors with less than three books)"); var shortestAverage = authors .Where(a => a.Books.Count() > 3) .Select(a => new { a.ID, a.Name, ShortestAverage = a.Books.Average(b => b.Title.Length) }).OrderBy(b => b.ShortestAverage); Console.WriteLine(shortestAverage.First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***5.What series has the most books:"); var authorsByLetter = authors .SelectMany(author => author.Books) .GroupBy(book => book.Series) .Select(book => new { book.Key, Count = book.Count() }) .OrderByDescending(book => book.Count); Console.WriteLine(authorsByLetter.First());//First is a empty string; Console.WriteLine(authorsByLetter.Skip(1).First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***6.Which year has the most books published:"); var mostPublishedYear = authors .SelectMany(author => author.Books) .GroupBy(book => book.Year) .OrderByDescending(book => book.Count()); var firstYear = mostPublishedYear.First(); Console.WriteLine($"In {firstYear.Key} year has the most published books: {firstYear.Count()} books"); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***7.The average number of books published for years in the 21st centrury?\n (Starting with 2001, not 2000):"); var century21Books = authors .SelectMany(author => author.Books) .Where(book => book.Year > 2000) .GroupBy(book => book.Year) .Average(book => book.Count()); Console.WriteLine($"The average number of books published in the 21st century is: {century21Books} books"); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***8.Author who has the most different series:"); var authorWithDiffSeries = authors.Select(a => new { a.ID, a.Name, NumbersOfDiffSeries = a.Books.GroupBy(b => b.Series).Distinct().Count() }).OrderByDescending(b => b.NumbersOfDiffSeries).ThenBy(b => b.Name); Console.WriteLine(authorWithDiffSeries.First());//There are two authors with the same number od different series; Console.WriteLine(authorWithDiffSeries.Skip(1).First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***9.Author who has the most books written that belong to a series:"); var booksWithSeries = authors.Select(author => new { author.ID, author.Name, BookWithSerie = author.Books.Where(book => !string.IsNullOrEmpty(book.Series)).Count() }).OrderByDescending(author => author.BookWithSerie); Console.WriteLine(booksWithSeries.First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***10.Author with the longest career:"); var longestCarrer = authors.Select(a => new { a.ID, a.Name, Sub = a.Books.Select(b => b.Year).Max() - a.Books.Select(b => b.Year).Min() }).OrderByDescending(a => a.Sub); Console.WriteLine(longestCarrer.First()); //Bonus /*1.What series has the most authors ? * 2.In Which year most authors published a book? * 3.Which author has the highest average books per year? * 4.How long is the longest hiatus between two books for an author, and by whom ?*/ Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***1.What series has the most authors:"); var mostAuthorsSeries = authors .SelectMany(author => author.Books) .Select(book => new { book.Title, Author = authors.Where(author => author.Books.Contains(book)).First().Name }).GroupBy(book => book.Title).OrderByDescending(book => book.Count()); var firstSeries = mostAuthorsSeries.First(); Console.WriteLine($" Series: {firstSeries.Key} => NumberOfAuthors:{firstSeries.Count()}"); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***2.In Which year most authors published a book:"); var yearMostPublished = authors .SelectMany(author => author.Books) .GroupBy(book => book.Year) .Select(year => year.Key) .Select(year => new { Year = year, NumberOfAuthors = authors.Select(author => new { Years = author.Books.Select(book => book.Year).Distinct() }).Where(a => a.Years.Contains(year)) }).OrderByDescending(x => x.NumberOfAuthors.Count()); var firstMostPublishedYear = yearMostPublished.First(); Console.WriteLine($"{firstMostPublishedYear.Year}Year => {firstMostPublishedYear.NumberOfAuthors.Count()} authors "); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***3.Which author has the highest average books per year:"); var highestAverageBooks = authors.Select(author => new { author.ID, author.Name, BooksInYear = author.Books.GroupBy(book => book.Year).Average(book => book.Count()) }).OrderByDescending(author => author.BooksInYear); Console.WriteLine(highestAverageBooks.First()); Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("***4.How long is the longest hiatus between two books for an author, and by whom:"); Console.ReadLine(); }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); #region FromClass ////var authors31 = new List<Author>(); ////foreach (var author in authors) ////{ //// if (author.BookCount == 31) //// { //// authors31.Add(author); //// } ////} ////var authors31 = authors.Where(Has31Books1); ////Func<Author, bool> has31books2 = author => author.BookCount == 31; ////var authors31 = authors.Where(has31books2); //var authors31 = authors.Where(author => author.BookCount == 31); //PrintAuthors(authors31); //PrintAuthors(authors.Where(a => a.Name.StartsWith("Jeff "))); //var between = authors // .Where(a => a.WweId >= 2200) // .Where(a => a.WweId <= 2300) // .Where(a => a.Name.StartsWith("A")) // .Where(a => a.BookCount > 2); //PrintAuthors(between); //var between2 = from a in authors // where a.WweId >= 2200 && a.WweId <= 2300 // where a.Name.StartsWith("A") // where a.BookCount > 2 // select a; //PrintAuthors(between2); //var moreThanNinetyEven1 = new List<Author>(); //var authorsArr = authors.ToArray(); //for (int index = 0; index < authorsArr.Length; index++) //{ // var author = authorsArr[index]; // if (author.BookCount > 90) // { // if (index % 2 == 0) // { // moreThanNinetyEven1.Add(author); // } // } //} //PrintAuthors(moreThanNinetyEven1); //var moreThanNinetyEven2 = authors // .Where((author, index) => index % 2 == 0) // .Where(author => author.BookCount > 90); //PrintAuthors(moreThanNinetyEven2); //var moreThanNinetyEven3 = authors // .Select((author, index) => new { // author.Name, // author.BookCount, // Index = index, // }) // .Where(author => author.Index % 2 == 0) // .Where(author => author.BookCount > 90); //PrintAuthors(moreThanNinetyEven3); #endregion // All Authors with split names into FirstName and LastName(added Jr. on last name) var nemaSpaces = authors .Select(author => new { author.Name, LastSpace = author.Name.LastIndexOf(" "), // FirstSpace = author.Name.IndexOf(" "), - because almost always last word is the last name HasSpace = author.Name.IndexOf(" ") != -1 }) .Select(author => new { author.Name, FirstName = author.HasSpace ? author.Name.Substring(0, author.LastSpace) : author.Name, LastName = author.HasSpace ? author.Name.Substring(author.LastSpace + 1) : "", }) .Select(author => new { // new object with Last name + "Jr." author.Name, FirstName = author.Name.Contains("Jr.") ? author.FirstName.Substring(0, author.FirstName.LastIndexOf(" ")) : author.FirstName, LastName = author.Name.Contains("Jr.") ? author.Name.Substring(author.FirstName.LastIndexOf(" ") + 1) : author.LastName }); // Print all authors with "Jr." added on lastname var authorsWithJr = nemaSpaces.Where(author => author.LastName.Contains("Jr.")); // PrintAuthors(authorsWithJr); // Print all authors only with one first name and last name var authorsWithOnlyOneFirst = nemaSpaces.Where(author => author.FirstName.IndexOf(" ") == -1); // PrintAuthors(authorsWithOnlyOneFirst); // Print authors name with more than one name var authorsNamesWithSpaces = nemaSpaces.Where(author => author.FirstName.IndexOf(" ") != -1); // PrintAuthors(authorsNamesWithSpaces); // Print authors with only name var authorsOnlyName = nemaSpaces .Where(author => author.Name.IndexOf(" ") == -1) .Select(author => new { author.Name }); // PrintAuthors(authorsOnlyName); // Print authors that has more than one firstname and contains "Jr." var authorsMoreNameWithJr = authorsWithJr.Where(author => author.FirstName.IndexOf(" ") != -1); PrintAuthors(authorsMoreNameWithJr); }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); Action <int> line = (input) => Console.WriteLine($"{input}--------------------"); line(1); // 1 What is the average number of books per autors? var averageBooks = authors.Average(a => a.Books.Count()); Console.WriteLine($"The average number of books by author is: {averageBooks}"); line(2); // 2 Which book(s) has the longest title, and how long is it? var bookLongestTitle = authors .SelectMany(a => a.Books) .OrderByDescending(b => b.Title.Length) .First(); Console.WriteLine($"The longest title from books is: {bookLongestTitle.Title} ({bookLongestTitle.Title.Length} characters)"); line(3); //3 Which author has the shortest average title for a book? var shortestAverageTitle = authors.Select(a => new { a.Name, shortestAverage = a.Books.Average(b => b.Title.Length) }).OrderBy(a => a.shortestAverage).First(); Console.WriteLine($"Author who has shortest average title for a book is : {shortestAverageTitle.Name} ({shortestAverageTitle.shortestAverage} characters)"); line(4); //4 Which author has the shortest average title for a book? (Discount authors with less than three books) var shortestAverage2 = authors.Where(a => a.Books.Count() >= 3).Select(x => new { x.Name, shortestAverage = x.Books.Average(b => b.Title.Length) }).OrderBy(a => a.shortestAverage).First(); Console.WriteLine($@"Author who has shortest average title for a book without authors with less than three books is : {shortestAverage2.Name} ({shortestAverage2.shortestAverage} characters)"); line(5); //5 What series has the most books? var seriesWithMostBooks = authors .SelectMany(a => a.Books) .Where(b => !string.IsNullOrEmpty(b.Series)) .GroupBy(b => b.Series) .OrderByDescending(b => b.Count()) .First(); Console.WriteLine($"Series with the most books is : {seriesWithMostBooks.Key} ({seriesWithMostBooks.Count()} books)"); line(6); // 6 Which year has the most books published? var yearMostBooks = authors.SelectMany(a => a.Books).GroupBy(b => b.Year).OrderByDescending(y => y.Count()).First(); Console.WriteLine($"The most books are published in: {yearMostBooks.Key}"); line(7); // 7 What is the average number of books published for years in the 21st centrury? (Starting with 2001, not 2000) var century21AverageBooks = authors.SelectMany(a => a.Books).Where(b => b.Year > 2000).GroupBy(b => b.Year).Average(x => x.Count()); Console.WriteLine($"The average number of books published in the 21st century is: {century21AverageBooks} books"); // This includes books to be published in 2020 line(8); // 8 Which author has the most different series? var authorMostDifferentSeries = authors.Select(a => new { a.Name, differentSeries = a.Books.GroupBy(b => b.Series).Count() - 1 }).OrderByDescending(b => b.differentSeries).ThenBy(a => a.Name).ToList(); Console.WriteLine($@"Authors with most different series are : {authorMostDifferentSeries[0].Name} ({authorMostDifferentSeries[0].differentSeries} series) {authorMostDifferentSeries[1].Name} ({authorMostDifferentSeries[1].differentSeries} series)"); // Two authors with same number of different series. line(9); // 9 Which author has the most books written that belong to a series? var authorWithMostSeries = authors.Select(a => new { a.Name, NumOfSeries = a.Books.Where(b => !string.IsNullOrEmpty(b.Series)).Count() }).OrderByDescending(a => a.NumOfSeries).First(); Console.WriteLine($"Author with most series is: {authorWithMostSeries.Name} ({authorWithMostSeries.NumOfSeries} series)"); line(10); // 10 Which author has the longest career? var authorLongestCareer = authors.Select(a => new { a.Name, Career = a.Books.OrderBy(b => b.Year).Select(b => b.Year).Last() - a.Books.OrderBy(b => b.Year).Select(b => b.Year).First() }).OrderByDescending(a => a.Career).First(); // In the authors list there is authors with more than 100 years(ex. daniel defoe 305) maybe because // some books are published later or it is mistike in years in authors.json file Console.WriteLine($"Author with longest career is: {authorLongestCareer.Name} ({authorLongestCareer.Career} years)"); line(11); // Bonus 1 What series has the most authors? var seriesWithMostAuthors = authors .SelectMany(a => a.Books.Select(b => new { b.Title, b.Series, Author = a.Name })) .Where(b => !string.IsNullOrEmpty(b.Series)) .GroupBy(b => b.Series) .Select(g => new { Series = g.Key, NumAuthors = g.Select(s => s.Author).Distinct().Count() }) .OrderByDescending(g => g.NumAuthors).First(); Console.WriteLine($"Series with most authors is : {seriesWithMostAuthors.Series} ({seriesWithMostAuthors.NumAuthors} authors)"); line(12); // Bonus 2 In Which year most authors published a book? var yearMostAuthorsPublished = authors .SelectMany(a => a.Books.Select(b => new { b.Title, b.Year, Author = a.Name })) .GroupBy(b => b.Year) .Select(g => new { Year = g.Key, NumberAuthors = g.Select(b => b.Author).Distinct().Count() }).OrderByDescending(y => y.NumberAuthors).First(); Console.WriteLine($"The year with most different authors pubslished a book is : {yearMostAuthorsPublished.Year} ({yearMostAuthorsPublished.NumberAuthors} authors)"); line(13); // Bonus 3 Which author has the highest average books per year? var authorHighetsAverage = authors.Select(a => new { a.Name, AverageBooks = a.Books.GroupBy(b => b.Year).Average(x => x.Count()) }).OrderByDescending(a => a.AverageBooks).First(); Console.WriteLine($"Author with highest average books per year is : {authorHighetsAverage.Name} ({authorHighetsAverage.AverageBooks} books)"); line(14); // Bonus 4 How long is the longest hiatus between two books for an author, and by whom? //var longestHiatus = authors.Select(a => new //{ // a.Name, // LongestBreak = BiggestDifference(a.Books.OrderByDescending(b => b.Year).Select(x => x.Year).ToList()) //}).OrderByDescending(a => a.LongestBreak).First(); var hiatus = authors.Select(a => new { a.Name, Years = a.Books.Select(b => b.Year).OrderBy(y => y) }).Select(a => new { a.Name, Hiatus = a.Years.Select((year, index) => { if (index == 0) { return(0); } return(year - a.Years.ElementAt(index - 1)); }).Max() }).OrderByDescending(a => a.Hiatus).Take(100).First(); Console.WriteLine($"The longest break between two books has : {hiatus.Name} ({hiatus.Hiatus} years)"); line(15); }
static void Main(string[] args) { BooksLoader loader = new BooksLoader(); var authors = loader.GetAllAuthors(); //var authors31 = new List<Author>(); //foreach (var author in authors) //{ // if (author.BookCount == 31) // { // authors31.Add(author); // } //} //var authors31 = authors.Where(Has31Books1); //Func<Author, bool> has31books2 = author => author.BookCount == 31; //var authors31 = authors.Where(has31books2); var authors31 = authors.Where(author => author.BookCount == 31); PrintAuthors(authors31); PrintAuthors(authors.Where(a => a.Name.StartsWith("Jeff "))); var between = authors .Where(a => a.WweId >= 2200) .Where(a => a.WweId <= 2300) .Where(a => a.Name.StartsWith("A")) .Where(a => a.BookCount > 2); PrintAuthors(between); var between2 = from a in authors where a.WweId >= 2200 && a.WweId <= 2300 where a.Name.StartsWith("A") where a.BookCount > 2 select a; PrintAuthors(between2); var moreThanNinetyEven1 = new List <Author>(); var authorsArr = authors.ToArray(); for (int index = 0; index < authorsArr.Length; index++) { var author = authorsArr[index]; if (author.BookCount > 90) { if (index % 2 == 0) { moreThanNinetyEven1.Add(author); } } } PrintAuthors(moreThanNinetyEven1); var moreThanNinetyEven2 = authors .Where((author, index) => index % 2 == 0) .Where(author => author.BookCount > 90); PrintAuthors(moreThanNinetyEven2); var moreThanNinetyEven3 = authors .Select((author, index) => new { author.Name, author.BookCount, Index = index, }) .Where(author => author.Index % 2 == 0) .Where(author => author.BookCount > 90); PrintAuthors(moreThanNinetyEven3); var nemaSpaces = authors //.Where(a => a.Name.IndexOf(" ") == -1) .Select(author => new { author.Name, FirstSpace = author.Name.IndexOf(" "), HasSpace = author.Name.IndexOf(" ") != -1 }) .Select(author => new { author.Name, FirstName = author.HasSpace ? author.Name.Substring(0, author.FirstSpace) : author.Name, LastName = author.HasSpace ? author.Name.Substring(author.FirstSpace + 1) : "" }) .Where(author => author.LastName.IndexOf(" ") != -1); PrintAuthors(nemaSpaces); Console.WriteLine(nemaSpaces.Count()); }