C#C
C#3w ago
AizakkuIV

IEnumerable<Author>

How do I return a list of authors (with their books) in a list and print that author and the books correctly?

  public IEnumerable<Author> GetAuthorsWithMostBooks()
  {
      int max = -1;
      Dictionary<Author, List<Book>> listOfAuthors = new Dictionary<Author, List<Book>>();

      foreach (KeyValuePair<string, List<Book>> author in _authorWithBooks)
      {
          List<Book> currentAuthor = author.Value;

          if (currentAuthor.Count > max)
          {
              max = currentAuthor.Count;
          }
      }

      foreach (KeyValuePair<string, List<Book>> author in _authorWithBooks)
      {
          List<Book> currentAuthor = author.Value;

          if (currentAuthor.Count == max)
          {
              listOfAuthors.Add(new Author(author.Key), author.Value);
          }
      }

      return listOfAuthors;
  }
Was this page helpful?