Search and filter
Write book search and filtering with LINQ.
Search by author
In this lesson we add a SearchByAuthor method to the Library class. It takes an author name and returns the list of books by that author. Inside, a foreach walks all books and compares each one's Author property.
We collect matches in a fresh List<Book>. Every matching book is Added there. At the end the method returns the list. This pattern is a plain version of what LINQ's Where does behind the scenes; writing it by hand makes the logic clear.
Building a result list
The common pattern in search methods is this: first create an empty result list, then walk the source collection with foreach and add items that pass the condition. You start with var result = new List<Book>();.
This pattern is called a filter. The condition is an equality check like if (b.Author == author); you can write more complex conditions too (for example, partial match with Contains). The result list can stay empty, which means the search found nothing.
Exercise Add the SearchByAuthor method
Add public List<Book> SearchByAuthor(string author) to the Library class. In the body start with var result = new List<Book>();; walk with foreach (var b in _books); do if (b.Author == author) result.Add(b);; finally return result;. In Main add a new book by Herbert and print the results of lib.SearchByAuthor("Herbert") in a foreach.
Show example solution
Add to Library public List<Book> SearchByAuthor(string author) { var result = new List<Book>(); foreach (var b in _books) { if (b.Author == author) result.Add(b); } return result; }. In Main add lib.AddBook(new Book { Title="Dune Messiah", Author="Herbert" }); and foreach (var b in lib.SearchByAuthor("Herbert")) Console.WriteLine(b.Title);
Example solution:
using System;
using System.Collections.Generic;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public bool IsBorrowed { get; set; }
}
class Member
{
public string Name { get; set; }
public int BorrowedCount { get; set; }
}
class Library
{
private List<Book> _books = new List<Book>();
private List<Member> _members = new List<Member>();
public void AddBook(Book b) { _books.Add(b); }
public void AddMember(Member m) { _members.Add(m); }
public void ListBooks()
{
foreach (var b in _books)
{
Console.WriteLine(b.Title);
}
}
public void Checkout(Member m, Book b)
{
if (!b.IsBorrowed)
{
b.IsBorrowed = true;
m.BorrowedCount++;
}
}
public void Return(Member m, Book b)
{
if (b.IsBorrowed)
{
b.IsBorrowed = false;
m.BorrowedCount--;
}
}
public List<Book> SearchByAuthor(string author)
{
var result = new List<Book>();
foreach (var b in _books)
{
if (b.Author == author) result.Add(b);
}
return result;
}
}
class Program
{
static void Main()
{
var book = new Book { Title = "The Hobbit", Author = "Tolkien" };
var member = new Member { Name = "Alex" };
Console.WriteLine(book.Title);
Console.WriteLine(member.Name);
var lib = new Library();
lib.AddBook(new Book { Title = "Dune", Author = "Herbert" });
lib.AddBook(new Book { Title = "Dune Messiah", Author = "Herbert" });
lib.ListBooks();
lib.AddMember(member);
lib.Checkout(member, book);
Console.WriteLine(book.IsBorrowed);
foreach (var b in lib.SearchByAuthor("Herbert"))
{
Console.WriteLine(b.Title);
}
}
}
Test yourself
-
Match each search piece to its job
- foreach
- Walks each book
- if
- Performs the comparison
- result.Add
- Collects a match
- return result
- Returns the list
-
Which is the correct condition for the search?
foreach (var b in _books) { if (______) result.Add(b); }- b.Title = author
- b.Author != author
- b.IsBorrowed
- b.Author == author
Show answer
b.Author == author — Correct. We compare author equality with ==.
-
Add to the result list
result.____(b);Show answer
result.Add(b);