title: Fastest way to search in a string collection
body: <p><strong>Problem:</strong></p>
  
  <p>I have a text file of around <strong>120,000</strong> users (strings) which I would like to store in a collection and later to perform a search on that collection.</p>
  
  <p>The search method will occur every time the user change the text of a <code>TextBox</code> and the result should be the strings that <strong><em>contain</em></strong> the text in <code>TextBox</code>.</p>
  
  <p>I don't have to change the list, just pull the results and put them in a <code>ListBox</code>.</p>
  
  <p><strong>What I've tried so far:</strong></p>
  
  <p>I tried with two different collections/containers, which I'm dumping the string entries from an external text file (once, of course):</p>
  
  <ol>
  <li><code>List&lt;string&gt; allUsers;</code></li>
  <li><code>HashSet&lt;string&gt; allUsers;</code></li>
  </ol>
  
  <p>With the following <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query">LINQ</a> query:</p>
  
  <p><code>allUsers.Where(item =&gt; item.Contains(textBox_search.Text)).ToList();</code></p>
  
  <p>My search event (fires when user change the search text):</p>
  
  <pre><code>private void textBox_search_TextChanged(object sender, EventArgs e)
  {
      if (textBox_search.Text.Length &gt; 2)
      {
          listBox_choices.DataSource = allUsers.Where(item =&gt; item.Contains(textBox_search.Text)).ToList();
      }
      else
      {
          listBox_choices.DataSource = null;
      }
  }
  </code></pre>
  
  <p><strong>Results:</strong></p>
  
  <p>Both gave me a poor response time (around 1-3 seconds between each key press).</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Where do you think my bottleneck is? The collection I've used? The search method? Both?</p>
  
  <p>How can I get better performance and more fluent functionality?</p>