Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

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?

like image 863
DannyD Avatar asked Nov 26 '25 16:11

DannyD


1 Answers

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

if(items.Any(x => x.CustomerID == 4 && x.DesignID == 6))
like image 160
Habib Avatar answered Nov 28 '25 17:11

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!