title: When will connection be closed in case of IEnumerable with using
body: <p>Suppose I have such pseudo code using some pseudo ORM (ok in my case it's Linq2Db).</p>
  
  <pre><code>static IEnumerable&lt;A&gt; GetA()
  {
    using (var db = ConnectionFactory.Current.GetDBConnection())
    {
       return from a in db.A
              select a;
    }
  }
  static B[] DoSmth()
  {
    var aItems = GetA();
    if (!aItems.Any())
      return null;
    return aItems.Select(a =&gt; new B(a.prop1)).ToArray();
  }
  </code></pre>
  
  <p>When will Connection be closed in <code>db</code>? Would it be closed at all in that case? What connection would be closed - those in using statement or those in lambda expression? .NET compiler is creating anonymous class for lambdas, so it will copy connection to that class. When would that connection be closed?</p>
  
  <p>Somehow I managed to get <code>Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.</code> I materialized queries and exception disappeared. But I'm wondering how this thing works.</p>