Ef Core receiving error
System.InvalidOperationException: Can't process set operations after client evaluation, consider moving the operation before the last Select() call (see issue #16243) at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplySetOperation(SetOperationType setOperationType, SelectExpression select2, Boolean distinct)
when executing
public async Task<object> GetUnitsForDataTableAsync() =>
await context.Units
.Where(x => !x.TractUnitJunctions.Any())
.Select(x => new
{
x.Id,
x.UnitName,
x.UnitAcres,
TractNum = String.Empty,
Wells = String.Empty,
NumOfWells = 0,
})
.Union(
context.TractUnitJunctions
.Select(x => new
{
Id = x.UnitId,
x.Unit.UnitName,
x.Unit.UnitAcres,
x.Tract.TractNum,
Wells = string.Join(", ", x.TractUnitJunctionWellJunctions
.Select(z => $"{z.Well.WellNum} ({z.Well.ApiNum})")
),
NumOfWells = x.TractUnitJunctionWellJunctions.Count()
}))
.ToListAsync().ConfigureAwait(false);
however the function works fine if I break it up into two queries.
public async Task<object> GetUnitsForDataTableAsync()
{
var List1 = await context.Units
.Where(x => !x.TractUnitJunctions.Any())
.Select(x => new
{
x.Id,
x.UnitName,
x.UnitAcres,
TractNum = String.Empty,
Wells = String.Empty,
NumOfWells = 0,
})
.ToListAsync().ConfigureAwait(false);
var List2 = await context.TractUnitJunctions
.Select(x => new
{
Id = x.UnitId,
x.Unit.UnitName,
x.Unit.UnitAcres,
x.Tract.TractNum,
Wells = string.Join(", ", x.TractUnitJunctionWellJunctions
.Select(z => $"{z.Well.WellNum} ({z.Well.ApiNum})")
),
NumOfWells = x.TractUnitJunctionWellJunctions.Count()
})
.ToListAsync().ConfigureAwait(false);
return List1.Concat(List2);
}
I've researched that error a bit but I'm unsure how to refactor the first query to get around that error