-1

Im getting red lines on comparestring1 && comparestring2, any idea why?

    List<string> comparestring1 = new List<string>();
    List<string> comparestring2 = new List<string>();

    foreach(string comparefinal in constrings)
    {
      if(comparestring1 && comparestring2 = "WORKING")
      {

      }
    }
jowey
  • 7,581
  • 6
  • 27
  • 46
  • 2
    You are getting a red line because your code is fundamentally wrong, You also haven't explained what you are trying to do, there is no question here. Try and spend some time being specific about what you are trying to do and edit the question – TheGeneral Feb 20 '19 at 08:26
  • Possible duplicate of [Comparing two collections for equality irrespective of the order of items in them](https://stackoverflow.com/questions/50098/comparing-two-collections-for-equality-irrespective-of-the-order-of-items-in-the) – Liam Feb 28 '19 at 14:52

2 Answers2

0

So essentially you just want to check whether a specific string present in both list or not. You can use Linq for that like

var data = constrings.Where(x => comparestring1.Contains(x) && comparestring2.Contains(x))
                     .ToList();

Your posted code is wrong in both syntactically and symantically. Thus the red line. Moreover, not sure what's the point in checking for a hard coded string on every iteration of another list. Anyway, your posted code should be like below using Linq using System.Linq

bool result = comparestring1.Any(x => x == "WORKING" && comparestring2.Contains(x))
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Worth to note that LINQ is a bit heavy on the memory and there are a few downsides performance wise, but it works! I don't think OP has any of that in mind but worth pointing out. +1 on this answer for simplicity. – Mark Denom Feb 20 '19 at 08:26
  • @MarkDenom I don't think so though ... even if you use a iteration block like `foreach` or `for` it would result in same performance most probably. – Rahul Feb 20 '19 at 08:27
  • In background of the newbieness of OP, I‘d suggest you also add, that you have to assign this to a variable, to can work with the result. – Nikolaus Feb 20 '19 at 08:31
0
        List<string> comparestring1 = new List<string>();
        List<string> comparestring2 = new List<string>();
        int i = 0;
        foreach (string comparefinal in constrings)
        {

            if (comparestring1[i] == "WORKING" && comparestring2[i] == "WORKING")
            {

            }
            i++;
        }

Try this ? you must choose index from List that you want to compare. And you need a definition for "constrings" ?