title: Building an external list while filtering in LINQ
body: <p>I have an array of input strings that contains either email addresses or account names in the form of domain\account.  I would like to build a List of string that contains only email addresses.  If an element in the input array is of the form domain\account, I will perform a lookup in the dictionary.  If the key is found in the dictionary, that value is the email address.  If not found, that won't get added to the result list.  The code below will makes the above description clear:</p>
  
  <pre><code>private bool where(string input, Dictionary&lt;string, string&gt; dict)
  {
      if (input.Contains("@"))
      {                
          return true;
      }
      else
      {
         try
         {
             string value = dict[input];             
             return true;
         }
         catch (KeyNotFoundException)
         {
             return false;
         }
      }
  }
  
  private string select(string input, Dictionary&lt;string, string&gt; dict)
  {
      if (input.Contains("@"))
      {                
          return input;
      }
      else
      {
          try
          {
              string value = dict[input];                    
              return value;
          }
          catch (KeyNotFoundException)
          {
              return null;
          }
      }
  }
  public void run()
  {
      Dictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;()
      {
          { "gmail\\nameless", "nameless@gmail.com"}
      };    
  
      string[] s = { "anonymous@gmail.com", "gmail\\nameless", "gmail\\unknown" };
      var q = s.Where(p =&gt; where(p, dict)).Select(p =&gt; select(p, dict));
      List&lt;string&gt; resultList = q.ToList&lt;string&gt;();
  }
  </code></pre>
  
  <p>While the above code works (hope I don't have any typo here), there are 2 problems that I do not like with the above:</p>
  
  <ol>
  <li>The code in where() and select() seems to be redundant/repeating.    </li>
  <li>It takes 2 passes.  The second pass converts from the query expression to List.</li>
  </ol>
  
  <p>So I would like to add to the List resultList directly in the where() method.  It seems like I should be able to do so.  Here's the code:</p>
  
  <pre><code>private bool where(string input, Dictionary&lt;string, string&gt; dict, List&lt;string&gt; resultList)
  {
      if (input.Contains("@"))
      {                
          resultList.Add(input);  //note the difference from above
          return true;
      }
      else
      {
         try
         {
             string value = dict[input];
             resultList.Add(value); //note the difference from above             
             return true;
         }
         catch (KeyNotFoundException)
         {
             return false;
         }
      }
  }
  </code></pre>
  
  <p>The my LINQ expression can be nicely in 1 single statement:</p>
  
  <pre><code>List&lt;string&gt; resultList = new List&lt;string&gt;();
  s.Where(p =&gt; where(p, dict, resultList));
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>var q = s.Where(p =&gt; where(p, dict, resultList)); //do nothing with q afterward
  </code></pre>
  
  <p>Which seems like perfect and legal C# LINQ.  The result: sometime it works and sometime it doesn't.  So why doesn't my code work reliably and how can I make it do so?    </p>