using System.Threading.Tasks;
using System.Collections.Generic;
public string Name { get; set; }
public string Author { get; set; }
public static List<Book> OrderByAuthorAndBookName()
return Books.OrderBy(book => book.Author).ThenBy(book => book.Name).ToList();
public static string[] GetAuthorsBookCount()
return Books.GroupBy(book => book.Author)
.Select(group => $"{group.Key} - Book count {group.Count()}")
public static Task<Book[]> GetBooksByAuthor(string author)
return Books.Where(book => book.Author == author).ToArray();
public static void DeleteAuthor(string authorName)
Books.RemoveAll(book => book.Author == authorName);
public static List<Book> Books { get; set; } = new List<Book>
new Book { Name = "Book1", Author = "Author1" },
new Book { Name = "Book3", Author = "Author3" },
new Book { Name = "Book4", Author = "Author3" },
new Book { Name = "Book1", Author = "Author2" },
new Book { Name = "Book3", Author = "Author1" },
new Book { Name = "Book1", Author = "Author3" },
new Book { Name = "Book2", Author = "Author2" },
new Book { Name = "Book2", Author = "Author1" },
new Book { Name = "Book2", Author = "Author3" },
new Book { Name = "Book3", Author = "Author2" },
public static void Main(string[] args)
var expectedOrderedBooks = new List<Book>
new Book { Name = "Book1", Author = "Author1" },
new Book { Name = "Book2", Author = "Author1" },
new Book { Name = "Book3", Author = "Author1" },
new Book { Name = "Book1", Author = "Author2" },
new Book { Name = "Book2", Author = "Author2" },
new Book { Name = "Book3", Author = "Author2" },
new Book { Name = "Book1", Author = "Author3" },
new Book { Name = "Book2", Author = "Author3" },
new Book { Name = "Book3", Author = "Author3" },
new Book { Name = "Book4", Author = "Author3" },
var expectedAuthorBookCounts = new[]
"Author1 - Book count 3",
"Author3 - Book count 4",
"Author2 - Book count 3",
var expectedBooksByAuthor1 = new[]
new Book { Name = "Book1", Author = "Author1" },
new Book { Name = "Book3", Author = "Author1" },
new Book { Name = "Book2", Author = "Author1" },
var expectedBooksAfterDeletion = new List<Book>
new Book { Name = "Book1", Author = "Author1" },
new Book { Name = "Book3", Author = "Author3" },
new Book { Name = "Book4", Author = "Author3" },
new Book { Name = "Book3", Author = "Author1" },
new Book { Name = "Book1", Author = "Author3" },
new Book { Name = "Book2", Author = "Author1" },
new Book { Name = "Book2", Author = "Author3" },
var orderedBooks = OrderByAuthorAndBookName();
Console.WriteLine("Ordered Books Validation:");
Console.WriteLine(orderedBooks.SequenceEqual(expectedOrderedBooks, new BookComparer()) ? "PASSED" : "FAILED");
var authorBookCounts = GetAuthorsBookCount();
Console.WriteLine("\nAuthor Book Counts Validation:");
Console.WriteLine(authorBookCounts.ToHashSet().SetEquals(expectedAuthorBookCounts) ? "PASSED" : "FAILED");
var booksByAuthor1 = GetBooksByAuthor("Author1").Result;
Console.WriteLine("\nBooks by Author1 Validation:");
Console.WriteLine(booksByAuthor1.ToHashSet(new BookComparer()).SetEquals(expectedBooksByAuthor1) ? "PASSED" : "FAILED");
Console.WriteLine("\nBooks After Deleting Author2 Validation:");
Console.WriteLine(Books.ToHashSet(new BookComparer()).SetEquals(expectedBooksAfterDeletion) ? "PASSED" : "FAILED");
public class BookComparer : IEqualityComparer<Book>
public bool Equals(Book x, Book y)
return x.Name == y.Name && x.Author == y.Author;
public int GetHashCode(Book obj)
return (obj.Name, obj.Author).GetHashCode();