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”?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
Any()method for this:This will go through your
bookcollection 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.