-1

here is what I got so for - maybe I'm doing this all wrong.

List<string> list2 = new List<string>() { "one", "two" };
List<string> list3 = new List<string>() { "three", "two"};


foreach (var item in list3)
{
    bool test = list2.Any(x => x == item);
    if (test == true)
    {
        Console.WriteLine("true");
    }
    else
    {
        Console.WriteLine("false");
    }
}

In this case it loops through list3 on the first Index and says - false - three is not in list2

Then it loops through list3 again and finds "two" which is true

What I would like is to only get false if there are no true statements. I need false to do something only if there no true statements

Or else another way to compare lists and if none of the values match each other then to run new code

Also I need it to tell the difference between something like hell and hello.


Here is how I got it to work

bool state = false;

foreach (var item in list3)
{
    bool test = list2.Any(x => x == item);
    if (test == true)
    {
        state = true;
    }
}
    if (state == true)
    {
        Console.WriteLine("true");
    }
    else
    {
        Console.WriteLine("false");
    }
jondavis4
  • 39
  • 4
  • Do you want to check if two lists have the exact same values in the same order (with the same list length)? – Progman Aug 07 '22 at 18:11

1 Answers1

0

if we use your code:

var state=false;
foreach (var item in list3)
{
  state = list2.Any(x => x == item);
  if (state == true)
  {
      break;
  }
}

...

Or

 var state = list3.Any(x=>list2.Any(y=>x==y));

"state" will be true or false. use wherever you want to use it.