title: How to determine if any element in a collection appears in a string with LINQ?
body: <p>I have <code>Dictionary&lt;string, List&lt;string&gt;&gt;</code> object. The <code>Key</code> represents the name of a file and the <code>Value</code> is a <code>List&lt;string&gt;</code> which represents the names of certain methods in the file.</p>
  
  <p>I loop over the dictionary and use the <code>Key</code> to read in data from the file. I'm trying to then find lines in this file which contain an element from the <code>Values</code> object:</p>
  
  <pre><code>static void FindInvalidAttributes(Dictionary&lt;string, List&lt;string&gt;&gt; dictionary)
  {
      //Get the files from my controller dir
      List&lt;string&gt; controllers = Directory.GetFiles(controllerPath, "*.cs", SearchOption.AllDirectories).ToList&lt;string&gt;();
  
      //Iterate over my dictionary
      foreach (KeyValuePair&lt;string, List&lt;string&gt;&gt; entry in dictionary)
      {
          //Build the correct file name using the dictionary key
          string controller = Path.Combine(ControllerPath, entry.Key + "Controller.cs");
  
          if (File.Exists(controller))
          {
              //Read the file content and loop over it
              string[] lines = File.ReadAllLines(controller);
              for (int i = 0; i &lt; lines.Count(); i++)
              {
                  //loop over every element in my dictionary's value (List&lt;string&gt;)
                  foreach (string method in entry.Value)
                  {
                      //If the line in the file contains a dictionary value element
                      if (lines[i].IndexOf(method) &gt; -1 &amp;&amp; lines[i].IndexOf("public") &gt; -1)
                      {
                          //Get the previous line containing the attribute
                          string verb = lines[i - 1];
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>There's got to be a cleaner way to implement the code inside the <code>if (File.Exists(controller))</code> statement. I don't want to nest a <code>foreach</code> inside of a <code>for</code>, inside the parent-most <code>foreach</code>.</p>
  
  <p><strong>Question:</strong> How can I determine if a <code>string</code> contains any element in a <code>List&lt;string&gt;</code> using LINQ?</p>
  
  <p>Note that the two values are not identical; part of the string should contain the entire list element. I was able to find plenty of examples looking for the string inside the list elements, but that is not what I'm trying to do.</p>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>lines[0] = "public void SomeMethod()";
  lines[1] = "public void SomeOtherMethod()";
  
  List&lt;string&gt; myList = new List&lt;string&gt;();
  myList.Add("SomeMethod");
  myList.Add("AnotherMethod");
  </code></pre>
  
  <p>Using the above data, <code>lines[0]</code> should result in my <code>FindInvalidAttributes</code> method looking at the previous line because this string contains the first element in <code>myList</code>. <code>lines[1]</code> should <strong>not</strong> result in the method inspecting the previous line because <code>SomeOtherMethod</code> does not appear in <code>myList</code>.</p>
  
  <p><strong>Edit</strong> I'm very curious why this was downvoted and marked to be closed for being "too broad". I'm asking a tremendously specific question, provided my code, sample data and expected output for the sample data.</p>