Searching if value exists in a list of objects using Linq

C#Linq

C# Problem Overview


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

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

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

zvolkov's answer is the perfect one to find out if there is such a customer. If you need to use the customer afterwards, you can do:

Customer customer = list.FirstOrDefault(cus => cus.FirstName == "John");
if (customer != null)
{
    // Use customer
}

I know this isn't what you were asking, but I thought I'd pre-empt a follow-on question :) (Of course, this only finds the first such customer... to find all of them, just use a normal where clause.)

Solution 3 - C#

One option for the follow on question (how to find a customer who might have any number of first names):

List<string> names = new List<string>{ "John", "Max", "Pete" };
bool has = customers.Any(cus => names.Contains(cus.FirstName));

or to retrieve the customer from csv of similar list

string input = "John,Max,Pete";
List<string> names = input.Split(',').ToList();
customer = customers.FirstOrDefault(cus => names.Contains(cus.FirstName));

Solution 4 - C#

Using Linq you have many possibilities, here one without using lambdas:

//assuming list is a List<Customer> or something queryable...
var hasJohn = (from customer in list
         where customer.FirstName == "John"
         select customer).Any();

Solution 5 - C#

customerList.Any(x=>x.Firstname == "John")

Solution 6 - C#

The technique i used before discovering .Any():

var hasJohn = (from customer in list
      where customer.FirstName == "John"
      select customer).FirstOrDefault() != null;

Solution 7 - C#

List<Customer> list = ...;
Customer john = list.SingleOrDefault(customer => customer.Firstname == "John");

john will be null if no customer exists with a first name of "John".

Solution 8 - C#

Try this, I hope it helps you.

 if (lstCustumers.Any(cus => cus.Firstname == "John"))
 {
     //TODO CODE
 }

Solution 9 - C#

Another possibility

if (list.Count(customer => customer.Firstname == "John") > 0) {
 //bla
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTony_HenrichView Question on Stackoverflow
Solution 1 - C#Andriy VolkovView Answer on Stackoverflow
Solution 2 - C#Jon SkeetView Answer on Stackoverflow
Solution 3 - C#Mike SacktonView Answer on Stackoverflow
Solution 4 - C#jmserveraView Answer on Stackoverflow
Solution 5 - C#Chris BrandsmaView Answer on Stackoverflow
Solution 6 - C#Ian BoydView Answer on Stackoverflow
Solution 7 - C#M4NView Answer on Stackoverflow
Solution 8 - C#Fabio StratottiView Answer on Stackoverflow
Solution 9 - C#KrassiView Answer on Stackoverflow