DannyD
DannyD

Reputation: 2881

How to check if two values exist in a list using linq in c#

I have a linq statement that looks like this:

if(items.Any(x => x.CustomerID == 4))
{

}

however, I want to find an object in my list of items that not only contains the customerID of 4 but also a designID of 6.

I know I can do this:

if(items.Any(x => x.CustomerID == 4) && items.Any(x => x.DesignID == 6))
{

}

but this may not work since I need to find the same object that has both these values (this would check individually if these exist). Is there a way to combine these?

Upvotes: 2

Views: 7015

Answers (1)

Habib
Habib

Reputation: 223207

You can combine two conditions like x.CustomerID == 4 && x.DesignID == 6

if(items.Any(x => x.CustomerID == 4 && x.DesignID == 6))

Upvotes: 9

Related Questions