I have dictionary which holds my books:
Dictionary<string, book> books
Book definiton:
class book
{
string author { get; set; }
string title { get; set; }
}
I have added some books to the dictionary.
How can I check if there is a book in the Dictionary that matches the title provided by the user?
books.ContainsKey("book name");
If you're not using the book title as the key, then you will have to enumerate over the values and see if any books contain that title.
foreach(KeyValuePair<string, book> b in books) // or foreach(book b in books.Values)
{
if(b.Value.title.Equals("some title", StringComparison.CurrentCultureIgnoreCase))
return true
}
Or you can use LINQ:
books.Any(tr => tr.Value.title.Equals("some title", StringComparison.CurrentCultureIgnoreCase))
If, on the other hand, you are using the books title as the key, then you can simply do:
books.ContainsKey("some title");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With