86 lines
2.3 KiB
C#
86 lines
2.3 KiB
C#
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<string> MatchBy(
|
|
IEnumerable<Book> books, string searchCriteria)
|
|
{
|
|
return books
|
|
.ExtractAuthors()
|
|
.ListAuthorsMatching(searchCriteria);
|
|
}
|
|
|
|
public static IEnumerable<string> SuggestAuthors(
|
|
IEnumerable<Book> books, int suggestionLength = 5)
|
|
{
|
|
return books
|
|
.ExtractAuthors()
|
|
.RandomizeOrder()
|
|
.Take(suggestionLength);
|
|
}
|
|
|
|
public static IEnumerable<Book> ListBooks(IEnumerable<Book> books, string author)
|
|
{
|
|
return books
|
|
.CatalogueByAuthor()
|
|
.BooksBy(author);
|
|
}
|
|
|
|
public static AuthorCatalogue CatalogueByAuthor(this IEnumerable<Book> books)
|
|
{
|
|
return books
|
|
.ToLookup(b => b.author)
|
|
// Mark Twain : {The Adventures of Huckleberry Finn},
|
|
// Shakespeare: {Othello, Hamlet, ...}
|
|
// ...
|
|
;
|
|
}
|
|
|
|
|
|
public static IEnumerable<Book> BooksBy(this AuthorCatalogue catalogue, string author)
|
|
{
|
|
return catalogue.Contains(author)
|
|
? catalogue[author]
|
|
: Enumerable.Empty<Book>();
|
|
}
|
|
|
|
public static IEnumerable<string> ExtractAuthors(this IEnumerable<Book> books)
|
|
{
|
|
return books.Select(b =>
|
|
{
|
|
//do stuff here
|
|
return b.author;
|
|
|
|
}).Distinct();
|
|
}
|
|
|
|
public static IEnumerable<string> ListAuthorsMatching(
|
|
this IEnumerable<string> authors, string searchCriteria)
|
|
{
|
|
var partOfNameLower = searchCriteria.ToLower();
|
|
return authors.Where(a => a.ToLower().Contains(partOfNameLower));
|
|
}
|
|
|
|
private static IOrderedEnumerable<string> RandomizeOrder(this IEnumerable<string> authors)
|
|
{
|
|
return authors.OrderBy(_ => Guid.NewGuid());
|
|
}
|
|
|
|
}
|
|
|
|
public class AuthorCatalogue
|
|
{
|
|
}
|
|
|
|
public class Book
|
|
{
|
|
|
|
}
|
|
}
|