title: Linq select into model and set properties
body: <p>In .NET Core 2.X, I was able to use this code below:</p>
  
  <pre><code>var bookings = await db.Tasks
          .Where(c =&gt; c.ClientId == clientId &amp;&amp; c.IsDeleted == false &amp;&amp; c.Start &gt; startOfThisMonth &amp;&amp; c.End &lt; endOfThisMonth)
          .OrderBy(x =&gt; x.Start)
          .Select(x =&gt; new SpecialTaskVm(new TaskViewModel(x, null))
          {
              client = x.Client,
              carer = x.Booking.SingleOrDefault(b =&gt; b.SlotNumber == 1).Carer,
              carer2 = x.Booking.SingleOrDefault(bk =&gt; bk.SlotNumber == 2).Carer
          })
          .ToListAsync();
  </code></pre>
  
  <p>However the same code in .net core 3.X results in this error:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: When called from 'VisitMemberInit', rewriting a node of type 'System.Linq.Expressions.NewExpression' must return a non-null value of the same type. Alternatively, override 'VisitMemberInit' and change it to not visit children of this type.</p>
  </blockquote>
  
  <p>I could really do with selecting in the way I do above as each model does some modification to some properties and each model is used elsewhere separately.</p>
  
  <p>I am also trying to avoid a <code>foreach</code> as it seems that would be inefficient.</p>
  
  <p>I have tried passing the properties I need to set, into the model and setting them in the model like that. Same error occurs.</p>
  
  <pre><code>//This action method will return data for current month.
  var startOfThisMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  var endOfThisMonth = new DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(1).Month, 1);
  
  var bookings = await db.Tasks
          .Where(c =&gt; c.ClientId == clientId &amp;&amp; c.IsDeleted == false &amp;&amp; c.Start &gt; startOfThisMonth &amp;&amp; c.End &lt; endOfThisMonth)
          .OrderBy(x =&gt; x.Start)
          .Select(x =&gt; new SpecialTaskVm(new TaskViewModel(x, null))
          {
              client = x.Client,
              carer = x.Booking.SingleOrDefault(b =&gt; b.SlotNumber == 1).Carer,
              carer2 = x.Booking.SingleOrDefault(bk =&gt; bk.SlotNumber == 2).Carer
          })
          .ToListAsync();
  </code></pre>
  
  <p>I expect for the list of tasks to be returned in the form of <code>List&lt;SpecialTaskVm&gt;</code> with <code>Client</code>, <code>Carer</code> and <code>Carer2</code> set.</p>