title: Setting all properties of dynamic object in anonymous type in linq query
body: <p>I need to convert dynamic objects (i.e. <code>List&lt;DynamicRow&gt;</code>) to a List of anonymous type objects but I need to have all the properties of the dynamic object to be created into an anonymous type object at run time either using linq or some other way.</p>
  
  <p>Is this possible? If so, how do I do this?</p>
  
  <p>This dynamic object is pretty much a grid class where it contains a list of columns i.e. <code>List&lt;String&gt;</code> and the rows i.e. <code>List&lt;DynamicRow&gt;</code>.</p>
  
  <p>I can access the value held in a DynamicRow object (Order for example) using the column name i.e. <code>Order[&lt;columnName&gt;] -&gt; Order["OrderNumber"]</code>, so I hope to be able to do the same using Linq.</p>
  
  <p>Is there a way to loop through the list of columns and create an anonymous object type for each row where each property is set based on the column name.</p>
  
  <p>While I know the code below doesn't work, it might make what I'm trying to achieve a bit clearer:</p>
  
  <p>So intead of having this:</p>
  
  <pre><code>var itemsList = from p in customerOrderCsvReader
  select new
  {                            
   Item = p.ElementAt(0).Value,
   Description = p.ElementAt(1).Value,
   Comment = p.ElementAt(2).Value
  };
  </code></pre>
  
  <p>I would like to have something like this as I won't know the Property name like Item, Description and Comment in advance. The only way I know what is in the dynamic object is by using the Column Name.</p>
  
  <pre><code>var itemsList = from p in customerOrderCsvReader
  select new
  {
   foreach string columnName in customerOrderCsvReader.columnNames
   {
    &lt;columnName as property&gt; = p.ElementAt(0).Value;
   }
  };
  </code></pre>
  
  <p>Another issue, assuming the above is doable, is there a way for me to access the property of p using a key i.e. columnName rather p.ElementAt. </p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>I probably should have been clearer. I'm trying to bind this to a grid, but I'm trying to also keep this generic so it will work on any platform, not just winform.</p>
  
  <p>If I use the code provided by @jjchiw, I get the following when outputting this to my output window:</p>
  
  <pre><code>Count = 6
      [0]: {System.Dynamic.ExpandoObject}
      [1]: {System.Dynamic.ExpandoObject}
      [2]: {System.Dynamic.ExpandoObject}
      [3]: {System.Dynamic.ExpandoObject}
      [4]: {System.Dynamic.ExpandoObject}
      [5]: {System.Dynamic.ExpandoObject}
  </code></pre>
  
  <p>But if I use linq and provide some dummy property names and assign some values to it using something like this:</p>
  
  <pre><code>var itemsList = from p in customerOrderCsvReader
                  select new
                  {
                    CustomerId = p.ElementAt(0),
                    EmployerId = p.ElementAt(1),
                    Description = p.ElementAt(2)
                  };
  </code></pre>
  
  <p>I get the following outputted to my output window:</p>
  
  <pre><code>Count = 6
      [0]: { CustomerId = "GREAL", EmployerId = "6", Description = "1997-05-06T00:00:00" }
      [1]: { CustomerId = "HUNGC", EmployerId = "3", Description = "1997-05-06T00:00:00" }
      [2]: { CustomerId = "LAZYK", EmployerId = "8", Description = "1997-05-06T00:00:00" }
      [3]: { CustomerId = "LETSS", EmployerId = "1", Description = "1997-05-04T00:00:00" }
      [4]: { CustomerId = "WALLM", EmployerId = "5", Description = "1997-05-04T00:00:00" }
      [5]: { CustomerId = "TOTAL", EmployerId = "4", Description = "1997-05-06T00:00:00" }
  </code></pre>
  
  <p>When binding the dynamic list to my grid, it doesn't display anything but when using the anonymous type list, which technically now contains a list of "recognisable" objects, my grid, displays the data accordingly.</p>
  
  <p>Is there anyway to force a dynamic list to behave like an anonymous one so that I can bind the dynamic one to my grid but get the correct results displayed.</p>