title: Convert IEnumerable<T> to collection of dynamically generated objects
body: <p>Recently I asked a more general question about getting properties of model through foreign key.
  Now I moved a bit further but still have no idea how transform objects on the fly.</p>
  
  <p>What I've got is an <code>IEnumerable</code> collection which I get through repository </p>
  
  <pre><code>regionRaw = unitOfWork.RegionRepository.Get(
      keyOrder: q =&gt; q.OrderBy(d =&gt; d.RegionID),
      filter: p =&gt; p.FullName.Contains(lastname) || p.ShortName.Contains(lastname),
      orderBy: jtSorting,
      includeProperties: "District, ISO31662, GOST767Region");
  </code></pre>
  
  <p>Further I am going to export data from this collection to Excel. So I need a select statement that gets all the fields I need.</p>
  
  <pre><code>dt = regionRaw
      .Select(x =&gt; new
      {
          ISO = x.ISO31662.GOSTName,
          DistrictName = x.District.ShortName
      })
  </code></pre>
  
  <p>I do not want to enumerate all the fields I need like on the top.  </p>
  
  <p>I am able to make a method that recognizes which of the fields have simple values and which have objects referenced through foreign key. And then that method will return a list of properties.</p>
  
  <p>Now I need some way to write something like a <code>foreach</code> inside <code>select</code>. I see something like this:</p>
  
  <pre><code>dt = regionRaw
       .Select(x =&gt; new
       {
           foreach (prop in propList)
           {
               prop.PropertyName = x.GetType()
                   .GetProperty(prop.TableName)
                   .GetValue(x, null).GetType()
                   .GetProperty(prop.PropertyName)
                   .GetValue(
                       x.GetType().GetProperty(prop.TableName).GetValue(x, null),
                       null);
           }
       }
  </code></pre>
  
  <p>Where <code>propList</code> is a collection of properties that I get before.</p>
  
  <p>I do totally realize that upper code is more a pseudo-code but I have no idea how to realize this in .NET. </p>
  
  <p>So if you can suggest some solution for this task I will be very grateful. Or maybe you could explain that all this is a bad idea and shouldn't be realized.</p>