Tony_Henrich Tony_Henrich - 8 months ago 36
C# Question

Searching if value exists in a list of objects using Linq

Say I have a class Customer which has a property FirstName. Then I have a List.

Can LINQ be used to find if the list has a customer with Firstname = 'John' in a single statement.. how?


LINQ defines an extension method that is perfect for solving this exact problem:

using System.Linq;
    bool has = list.Any(cus => cus.FirstName == "John");

make sure you reference System.Core.dll, that's where LINQ lives.