I have a collection of Book objects called book. The Book class has a field called Title.
Is there an easy way using Linq (or other) to find out if that collection has a Book object with a title of "Harry"?
You can use the Any()
method for this:
book.Any(b => string.Equals(b.Title, "Harry"));
This will go through your book
collection until it finds a book with the Title "Harry" or the end of your collection. If it finds a book with the correct title it stops going through your collection and returns true. If it reaches the end of your collection it returns false.
Edit: Please note, this does a culture-insensitive equality check. You might want to do a culture-sensitive one instead depending on your use-case.
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