using System; using System.Collections.Generic; using System.Data.SqlTypes; using System.Linq; using System.Text; namespace Books.ConsoleApp { public static class SearchByAuthor { public static IEnumerable MatchBy( IEnumerable books, string searchCriteria) { return books .ExtractAuthors() .ListAuthorsMatching(searchCriteria); } public static IEnumerable SuggestAuthors( IEnumerable books, int suggestionLength = 5) { return books .ExtractAuthors() .RandomizeOrder() .Take(suggestionLength); } public static IEnumerable ListBooks(IEnumerable books, string author) { return books .CatalogueByAuthor() .BooksBy(author); } public static AuthorCatalogue CatalogueByAuthor(this IEnumerable books) { return books .ToLookup(b => b.author) // Mark Twain : {The Adventures of Huckleberry Finn}, // Shakespeare: {Othello, Hamlet, ...} // ... ; } public static IEnumerable BooksBy(this AuthorCatalogue catalogue, string author) { return catalogue.Contains(author) ? catalogue[author] : Enumerable.Empty(); } public static IEnumerable ExtractAuthors(this IEnumerable books) { return books.Select(b => { //do stuff here return b.author; }).Distinct(); } public static IEnumerable ListAuthorsMatching( this IEnumerable authors, string searchCriteria) { var partOfNameLower = searchCriteria.ToLower(); return authors.Where(a => a.ToLower().Contains(partOfNameLower)); } private static IOrderedEnumerable RandomizeOrder(this IEnumerable authors) { return authors.OrderBy(_ => Guid.NewGuid()); } } public class AuthorCatalogue { } //public class Book //{ //} }