Search code examples
c#arrayslinqstring-comparison

Comparing 2 string arrays using Linq and Regex for a partial match


I have an array of search words and a second array that needs to be searched, returning true or false depending if any of the strings in the 2nd array contain text from the first. I tried using linq but I can't get it to return true if any of the values in the second array contain more than just those search words. I was thinking of using Regex maybe in combination with the linq query but I'm not sure how that would work. Here is what I was tried

string[] gsdSearchVerbiage =
  {
    "grade", 
     "transcript",
     "gsd"
  };

  string[] tableColumns = new string []
  {
    "maxsgrades",
     "somethingElse",
   };

bool gsdFound = tableColumns.Any(
   x => gsdSearchVerbiage.Contains(x));

This returns false. I understand why it's returning false but I can't figure out how to fix it. If the answer is to use a regular expression I couldn't figure out how to do that over 2 arrays...


Solution

  • Last statement should be

    bool gsdFound = tableColumns.Any(
        x => gsdSearchVerbiage.Any(y => x.Contains(y)));
    

    since you're trying to know if the current item of tableColumns (x) contains any of gsdSearchVerbiage words (y)