In .NET Core 2.X, I was able to use this code below:
var bookings = await db.Tasks
.Where(c => c.ClientId == clientId && c.IsDeleted == false && c.Start > startOfThisMonth && c.End < endOfThisMonth)
.OrderBy(x => x.Start)
.Select(x => new SpecialTaskVm(new TaskViewModel(x, null))
{
client = x.Client,
carer = x.Booking.SingleOrDefault(b => b.SlotNumber == 1).Carer,
carer2 = x.Booking.SingleOrDefault(bk => bk.SlotNumber == 2).Carer
})
.ToListAsync();
However the same code in .net core 3.X results in this error:
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.
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.
I am also trying to avoid a foreach
as it seems that would be inefficient.
I have tried passing the properties I need to set, into the model and setting them in the model like that. Same error occurs.
//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 => c.ClientId == clientId && c.IsDeleted == false && c.Start > startOfThisMonth && c.End < endOfThisMonth)
.OrderBy(x => x.Start)
.Select(x => new SpecialTaskVm(new TaskViewModel(x, null))
{
client = x.Client,
carer = x.Booking.SingleOrDefault(b => b.SlotNumber == 1).Carer,
carer2 = x.Booking.SingleOrDefault(bk => bk.SlotNumber == 2).Carer
})
.ToListAsync();
I expect for the list of tasks to be returned in the form of List<SpecialTaskVm>
with Client
, Carer
and Carer2
set.
SpecialTaskVm
objects.