title: Using async / await with DataReader ? ( without middle buffers!)
body: <p>My goal is simple , I want to do Asynchronous I/O calls (using async await)  - but : </p>
  
  <ul>
  <li>Without using DataFlow dependency ( <a href="https://stackoverflow.com/a/13148683/859154"><em>like in this answer</em></a>)</li>
  <li>Without middle buffers( <a href="https://stackoverflow.com/a/13148683/859154"><em>not like this answer</em></a>)</li>
  <li>The Projector function should be sent as an argument. ( <a href="https://stackoverflow.com/a/13149492/859154"><em>not like this answer</em></a>)</li>
  </ul>
  
  <p>Ok.</p>
  
  <p>Currently here is my code which it's job is to read from db and project each line to a <code>Func&lt;&gt;</code></p>
  
  <pre><code>public IEnumerable &lt; T &gt; GetSomeData &lt; T &gt; (string sql, Func &lt; IDataRecord, T &gt; projector)
  {
      using(SqlConnection _conn = new SqlConnection(@"Data Source=..."))
      {
          using(SqlCommand _cmd = new SqlCommand(sql, _conn))
          {
              _conn.Open();
              _cmd.CommandTimeout = 100000;
              using(IDataReader rdr = _cmd.ExecuteReader())
              {
                  while (rdr.Read())  yield    return projector(rdr);
              }
          }
      }
  }
  </code></pre>
  
  <p>So  , what is projector ? </p>
  
  <p>Each class has a function which gets a <code>record</code> ( <code>IDataRecord</code>) and create an entity : </p>
  
  <p>Example : </p>
  
  <pre><code>public class MyClass
  {
      public static MyClass MyClassFactory(IDataRecord record)
      {
          return new MyClass
          {
              Name = record["Name"].ToString(),
              Datee = DateTime.Parse(record["Datee"].ToString()),
              val = decimal.Parse(record["val"].ToString())
          };
      }
      public string Name    {   get;   set;  }
      public DateTime Datee    {  get;     set;  }
      public decimal val    {  get;    set;    }
  }
  </code></pre>
  
  <p>So here ,  <code>MyClassFactory</code> would be the <code>Func</code></p>
  
  <p>So how I currently run it ? </p>
  
  <pre><code> var sql = @"SELECT TOP 1000 [NAME],[datee] ,[val]  FROM [WebERP].[dbo].[t]";
   var a = GetSomeData &lt; MyClass &gt; (sql, MyClass.MyClassFactory).Where(...); //notice the Func
  </code></pre>
  
  <p><strong>All ok.</strong></p>
  
  <p>The problems starts now : </p>
  
  <p>Adding <code>async</code> to the method  yields an error : ( Yes I know that Ienumerable is a <em>Synchronous</em> interface hence the problem)</p>
  
  <p><code>public async Task&lt;IEnumerable &lt; T &gt;&gt; GetSomeData &lt; T &gt; (string sql, Func &lt; IDataRecord, T &gt; projector)</code></p>
  
  <blockquote>
    <p>cannot be an iterator block because
    'System.Threading.Tasks.Task>'
    is not an iterator interface type</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/a/13149492/859154"><em>But this guy here did</em></a> -  : </p>
  
  <p><img src="https://i.stack.imgur.com/UPnTv.jpg" alt="enter image description here"></p>
  
  <p>Which <strong>DOES</strong> compile.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I convert <strong>my code</strong> to support fully asynchronse IO call ?</p>
  
  <p><em>(under the conditions : without DataFlow dependency , send projector function as argument , no middle buffers)</em></p>