title: Dynamic LINQ expression for Select with child collection (Entity Framework)
body: <p>I would like to ask you about a way to dynamically create a LINQ Select expression with a nested child collection. Fields in the selected child collection can be static, however I would like to dynamically pass a list of fields in the current entity, and also fields in other entities, referenced by navigation properties.
  Here's the static version of the query, similar to the one which I use in my code, in many places, and which I would like to create dynamically:</p>
  <pre><code>var listItems = _efDbContext.Blogs.Select(x =&gt; new {      
      ID = x.ID,
      Name = x.Name, //field from the current entity     
      AuthorName = x.Author.Name, //field referenced by navigation property
      ...&lt;other fields from current or referenced entities(like AuthorName above), passed dynamically&gt;
      Posts = x.Posts.Select(y =&gt; new { //this select is 'static', is the same for other queries 
         Id = x.Id,
         Name = x.Name
      })
  });
  </code></pre>
  <p>I tried to figure out something from the answers in these two posts bewlo but I did not succeed. Answer by Ivan Stoev is really cool but it does not support referenced properties and I also have this static nested child collection select in my example above</p>
  <p><a href="https://stackoverflow.com/questions/38505904/dynamically-build-select-list-from-linq-to-entities-query">Dynamically build select list from linq to entities query</a></p>
  <p><a href="https://stackoverflow.com/questions/56370829/ef-linq-dynamic-lambda-expression-trees">EF Linq- Dynamic Lambda expression trees</a></p>
  <p>I have also tried to accomplish this using libraries like Dynamic.Linq.Core or Automapper but it seems that the former does not support select with child collections and the latter needs a DTO class and I dont want to create hundred of DTOs for each combination of fields in the select expression.</p>
  <p>I also thought that this whole approach might be overcomplicated and maybe I should use parametrized SQL query instead, and that's definitely a possible option, but I think that I could reuse this dynamic LINQ expression in different variations, in other places in my project, and it would be helpful in a long run :)</p>
  <p>(I'm using EF Core 3.1)</p>