title: Using a LINQ Where query to get only some of the ConfigurationManager.ConnectionStrings
body: <p>My goal is to use a LINQ <code>Where</code> query on the <code>ConfigurationManager.ConnectionStrings</code> collection in a console application (assume a fresh .NET 4.5 console application with a System.Configuration  reference added, and a correspodning <code>using</code> statement). </p>
  
  <p>I started with this, <strong>which does <em>not</em> work</strong>:</p>
  
  <pre><code>var relevantSettings =
          ConfigurationManager.ConnectionStrings.Where(s =&gt; s.Name.StartsWith("Xyz"));
  </code></pre>
  
  <p>It tells me that:</p>
  
  <blockquote>
    <p>The type arguments for method '<code>IEnumerable&lt;TSource&gt; System.Linq.Enumerable.Where&lt;TSource(this IEnumerable&lt;TSource&gt;, Func&lt;TSource,bool&gt;)</code>' cannot be inferred from the usage. Try specifying the arguments explicitly.</p>
  </blockquote>
  
  <p>This caught me off guard, so I tried this to check my sanity, but <strong>this does <em>not</em> work</strong> either:</p>
  
  <pre><code>foreach (var settingsin ConfigurationManager.ConnectionStrings)
  {
      if (settings.Name.StartsWith("Xyz")) Console.WriteLine("Found one!");
  }
  </code></pre>
  
  <p>It complains <em>not</em> about the <code>foreach</code> statement, but about the <code>.Name</code> bit, with the error:</p>
  
  <blockquote>
    <p>Could not resolve symbol 'Name'</p>
  </blockquote>
  
  <p>I understand that there's probably something preventing the compiler from inferring the <code>var</code>'s type, to double check I tried this <strong>which <em>does</em> work</strong>:</p>
  
  <pre><code>foreach (ConnectionStringSettings settings in ConfigurationManager.ConnectionStrings)
  {
      if (connectionString.Name.StartsWith("Xyz")) Console.WriteLine("Found one!");
  }
  </code></pre>
  
  <p>However, that doesn't help me very much, except for solving my immediate problem. I want to <em>understand</em> what is going on here.</p>
  
  <p>All I wanted to do is use a simple LINQ <code>Where</code> statement to get a subset of the connection strings in my app.config. Why is the compiler preventing me from doing this?</p>