title: print one line from foreach in c#
body: <p>I am trying to print the symbol (trimmedLine) and value from a list (matchedSymbols), but instead, it prints all the values of all the valid symbols for each symbol. I tried not using the foreach loop, but then it prints the value for the last symbol searched, which is invalid. Below is my current code for the method:</p>
  
  <pre><code>public void searchSymbolTable()
      {
          string searchingForFile = "";
          string[] searchFile;
  
          Console.Write("Enter the name of the search file: ");
          searchingForFile = @"../../" + Console.ReadLine();
          Console.WriteLine("Searching symbol table...");
          Console.WriteLine();
  
          while (!File.Exists(searchingForFile))
          {
              Console.Write("File not found, try again or enter 1 to exit: ");
              searchingForFile = @"../../" + Console.ReadLine();
              if (searchingForFile == @"../../1")
              {
                  Environment.Exit(1);
              }
          }
  
          searchFile = File.ReadAllLines(searchingForFile);
  
  
          matchedSymbols = new List&lt;symbols&gt;();
          var space = ' ';
  
          foreach (string line in searchFile)
          {
              try
              {
                  var trimmedLine = line.Trim();
                  var tokens = trimmedLine.Split(space);
  
                  trimmedLine = checkSymbol(tokens[0]);
  
                  matchedSymbols.Add(mySymbols.ToList().Find(symName =&gt; symName.ToString() == trimmedLine));
  
  
                  if (matchedSymbols[matchedSymbols.Count() - 1] == null)
                  {
                      throw new Exception(string.Format("ERROR - Symbol {0} not found in symbol table.", trimmedLine));
                  }
                  else
                  {
                     foreach(symbols s in mySymbols)
                     {
  
                             Console.WriteLine(string.Format("{0,-10} {1}", trimmedLine, s.value));
  
                     }
  
                  }
              }
              catch (Exception e)
              {
  
                  Console.WriteLine(e.Message);
                  if (Console.CursorTop % 20 == 19)
                  {
                      Console.Write("Press any key to continue ...");
                      Console.ReadKey();
                  }
  
              }
          }
      }
  </code></pre>