title: Converting a IEnumerable<T> to IObservable<T>, with maximum parallelism
body: <p>I have a sequence of async tasks to do (say, fetch N web pages). Now what I want is to expose them all as an <code>IObservable&lt;T&gt;</code>. My current solution uses the answer from <a href="https://stackoverflow.com/questions/13500456/how-to-convert-an-ienumerabletaskt-to-iobservablet">this question</a>:</p>
  
  <pre><code>async Task&lt;ResultObj&gt; GetPage(string page) {
      Console.WriteLine("Before");
      var result = await FetchFromInternet(page);
      Console.WriteLine("After");
      return result;
  }
  
  // pages is an IEnumerable&lt;string&gt;
  IObservable&lt;ResultObj&gt; resultObservable =pages.Select(GetPage).
                   Select(t =&gt; Observable.FromAsync(() =&gt; t)).Merge();
  
  // Now consume the list
  foreach(ResultObj obj in resultObservable.ToEnumerable()) {
      Console.WriteLine(obj.ToString());
  }
  </code></pre>
  
  <p>The problem is that I do not know the number of pages to be fetched, and it could be large. I do not want to make hundreds of simultaneous requests. So I want a way to limit the maximum number of tasks that will be executed in parallel. Is there a way to limit the number of concurrent invocations of <code>GetPage</code>?</p>
  
  <p>There is a <code>Merge</code> overload that takes a maxConcurrent parameter, but it does not seem to actually limit the concurrency of the function invokation. THe console prints all the Before messages before the After messages.</p>
  
  <p>Note: I need to convert back to <code>IEnumerable&lt;T&gt;</code>. I'm writing a data source for a system that gives me descriptors of data to fetch, and I need to give it back a list of the downloaded data.</p>