title: Improving performance of generic conversion from DataRow to T
body: <p>When querying more than 10k data sets from a <code>MSSQL</code> database our software gets pretty slow (more than 10sec loading time). I digged deeper just to find out that they implemented some <code>Entity</code>-like generic object conversion from <code>DataRow</code> to <code>T</code> by calling the contructor of the target type which accepts a <code>DataRow</code>. </p>
  
  <p>This loos like this:</p>
  
  <pre><code>        List&lt;T&gt; filteredList = new List&lt;T&gt;();
          DataTable dt = ((ITableReader)typeof(T).GetMethod("GetReader").Invoke(null, new object[]{})).GetDataWhere(args);
  
          Type[] types = new Type[] { typeof(DataRow) };
          var constructorInfo = typeof(T).GetConstructor(types);
          var parameters = Expression.Parameter(typeof(DataRow), "dataRow");
  
          Func&lt;DataRow, T&gt; Instance = Expression.Lambda&lt;Func&lt;DataRow, T&gt;&gt;(
                                          Expression.New(constructorInfo, parameters), parameters).Compile();
  
          foreach (DataRow row in dt.Rows)
          {
              filteredList.Add(Instance(row));
          }
  
          return filteredList;
  </code></pre>
  
  <p>The critical part is the <code>foreach</code> where for each row the contructor of the target type is called. Let's assume I have more than 10k data sets, it needs to iterate 10k times and initialize the object each time.</p>
  
  <p>I was thinking about how to improve this but I can't really find a solution. It seems like this needs some general refactoring by avoiding this transformation.</p>
  
  <p>My question therefore is, is there any way to improve the peformance in this case? If not, what should be done otherwise if I want a <code>List</code> of objects from my database. Just work with <code>DataRow</code>?</p>
  
  <p>Edit: Another Info, we are limited to .NET 4.0</p>