title: List<T> LINQ Projection to Anonymous or Dynamic type
body: <p>I'm trying to convert a <code>List&lt;Topic&gt;</code> to an anonymous or dynamic type via linq projection... I'm am using the following code, but it doesn't seem to work properly. It returns the dynamic type  without error, however, if I try to enumerate the children field (<code>list&lt;object/topic&gt;</code>) then it says </p>
  
  <blockquote>
    <p>Results View = The type <code>'&lt;&gt;f__AnonymousType6&lt;id,title,children&gt;'</code> exists in both 'MyWebCore.dll' and 'MvcExtensions.dll'</p>
  </blockquote>
  
  <p>Strange. </p>
  
  <p>Here is the code I am using: </p>
  
  <pre><code>protected dynamic FlattenTopics()
  {
      Func&lt;List&lt;Topic&gt;, object&gt; _Flatten = null; // satisfy recursion re-use
      _Flatten = (topList) =&gt;
      {
          if (topList == null) return null;
  
          var projection = from tops in topList
                           select new
                           {
                               id = tops.Id,
                               title = tops.Name,
                               children = _Flatten(childs.Children.ToList&lt;Topic&gt;())
                           };
          dynamic transformed = projection;
          return transformed;
      };
  
      var topics = from tops in Repository.Query&lt;Topic&gt;().ToList()
                   select new
                   {
                       id = tops.Id,
                       title = tops.Name,
                       children = _Flatten(tops.Children.ToList&lt;Topic&gt;())
                   };
  
      return topics;
  }
  </code></pre>
  
  <p>All i'm doing is flattening a list of self containing objects - basically it's a list of POCOs that will be stuffed into a tree view (jstree).  </p>
  
  <p>The Topic class is defined as:</p>
  
  <pre><code>public class Topic
  {
      public Guid Id {get;set;}
      public string Name {get;set;}
      public List&lt;Topic&gt; Children {get;set;}
  }
  </code></pre>
  
  <p>And here is an example of what the first member of the returned dynamic object looks like: </p>
  
  <pre><code>[0] = { 
      id = {566697be-b336-42bc-9549-9feb0022f348},
      title = "AUTO", 
      children = {System.Linq.Enumerable.SelectManyIterator
            &lt;MyWeb.Models.Topic,
             MyWeb.Models.Topic,
             &lt;&gt;f__AnonymousType6&lt;System.Guid,string,object&gt;
            &gt;} 
   }
  </code></pre>