0

Continuation of previous question linq where list contains any in list

I have a list of objectA Where ObjectA has 2 properties - string action,bool valid

Ex action =abc, valid = true
action xyz , valid = false

etc

I have two local variables

string actiontocheck ="abc"

string validitychecker ="somevalidvalue" // it can be something else too

I want to check something like Iterate through list .

if any of the action in the list contains actiontocheck and 
the corresponding valid flag is true, 

if(validitychecker == "somevalidvalue")
  set flag to false

What I have tried

 if (List1.Any(a => (a.Action.Contains(actiontocheck)) && a.valid)
        {
            if (validitychecker == "somevalidvalue")
            {
                Ignore = true;
            }
        }
Community
  • 1
  • 1
KeenUser
  • 5,305
  • 14
  • 41
  • 62

1 Answers1

0

The issue is the type of a.valid. There are many ways to solve this issue, but if you know that it will have a value of True or False every time, you should be able to simply write:

if (List1.Any(a => (a.Action.Contains(actiontocheck)) && a.valid.Value)
{
    if (validitychecker == "somevalidvalue")
    {
        Ignore = true;
    }
}
grovesNL
  • 6,016
  • 2
  • 20
  • 32