title: Linq.Dynamic ICollection Navigation Property Error: "Value cannot be null. Parameter name: expression"
body: <p>I've been really struggling with this one for the better part of today and would seriously appreciate any help!</p>
  <p>I am using Entity Framework and have two related classes, ac_File and ac_Space which are related on DefaultSpace (fk in ACFile table) and  DefaultFile (fk in ACSpace table) with a one to many relationship from ACFile -&gt; ACSpace.</p>
  <p><a href="https://i.stack.imgur.com/xfjcU.png" rel="nofollow noreferrer">Limited view of ACFile &lt;-&gt; ACSpaces EF Relationship diagram</a></p>
  <p>The truncated class code showing relevant fields is here:</p>
  <pre><code>public partial class ACFile
  {
      public System.Guid Id { get; set; }
      public virtual ICollection&lt;ACSpace&gt; DefaultSpaces { get; set; }
  }
  
  public partial class ACSpace
  {
  
      public int Id { get; set; }
      public virtual ACFile DefaultPhoto { get; set; }
  
  }
  
  
  
  //streamlined class for transferring data
  public partial class ACFileDTO
  {
      public System.Guid Id { get; set; }
      public IEnumerable&lt;System.Int32?&gt; DefaultSpacesId { get; set; }
  }
  </code></pre>
  <p>I am trying to standardize our API and enable dynamic querying. As part of this process, I am creating DTO code which enables construction of dynamic queries through the <a href="https://dynamic-linq.net/" rel="nofollow noreferrer">System.Dynamic.Linq library</a>. The DTO objects are simple and only retain ID references to related objects.</p>
  <p>In this scenario, I am trying to select data from my ORM object (ACFile) into the DTO (ACFileDTO) and select only the exact fields necessary to populate the ACFileDTO class. This means only grabbing the Id from any related ACSpace objects based on the DefaultSpace &lt;-&gt; DefaultPhoto relationship.</p>
  <p>If I run my query like this, using the standard LINQ select method with a lambda, it runs fine:</p>
  <pre><code>List&lt;ACFileDTO&gt; fileList = EE.ACFileSet.Where(whereLinqExpression).Select(x =&gt;  new ACFileDTO()
  {
      Id = x.Id,
      DefaultSpacesId = x.DefaultSpaces.Select(p =&gt; (System.Int32?)p.Id),
  } 
  ).ToList();
  </code></pre>
  <p>but if I build my Select statement as a dynamic string and execute it through the following code:</p>
  <pre><code>string dynamicSelectStatement = ACFileDTO.GetDynamicSelectStatement(&quot;&quot;, &quot;&quot;, sourceAbbreviation: &quot;&quot;);
  List&lt;ACFileDTO&gt; fileList = EE.ACFileSet.Where(whereLinqExpression).Select&lt;ACFileDTO&gt;(dynamicSelectStatement).ToList();
  </code></pre>
  <p>then, ONLY when the DefaultSpacesId field is included, it fails with the following message:</p>
  <pre><code>&quot;Message&quot;: &quot;An error has occurred.&quot;,
  &quot;ExceptionMessage&quot;: &quot;Value cannot be null.\r\nParameter name: expression&quot;,
  &quot;ExceptionType&quot;: &quot;System.ArgumentNullException&quot;,
  &quot;StackTrace&quot;: &quot;   at System.Linq.Expressions.Expression.RequiresCanRead(Expression expression, String paramName)....(continued...will post full message if necessary)&quot;
  </code></pre>
  <p>I have tried MANY permutations of the DefaultSpacesId portion of the query (with and without null ternary operators, using the np() null propagating operator, using .Any() to check the collection, etc), but with all of these changes I still get the same error.</p>
  <p>The gist of the dynamic query I want to write is:</p>
  <pre><code>List&lt;ACFileDTO&gt; fileList = EE.ACFileSet.Where(whereLinqExpression).Select&lt;ACFileDTO&gt;(&quot;new ( Id, (DefaultSpaces != null ?  DefaultSpaces.Select(new ( Id ) ).ToList() : null ) as DefaultSpacesId )&quot;).ToList();
  </code></pre>
  <p>I just started using the Dynamic Linq library last week so hopefully I am just getting the syntax wrong or missing some simple thing, any help is seriously appreciated!</p>