This is my SQL query and I tested it in linqpad, and it worked, but it doesn't work in EF Core 3.1:
from v in JournalVoucherLines
group v by v.AccountId into vg
join bp in Parties on vg.FirstOrDefault().AccountId equals bp.AccountId
select new
{
name = bp.FullName,
key = vg.Key,
Creditor = vg.Sum(v => v.Credit),
Deptor = vg.Sum(v => v.Debit),
RemainAmount = vg.Sum(v => v.Credit) - vg.Sum(v => v.Debit)
}
When I use the query in EF Core, I get this exception:
The LINQ expression '(GroupByShaperExpression: KeySelector: (j.AccountId), ElementSelector:(EntityShaperExpression: EntityType: JournalVoucherLine ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ).FirstOrDefault()'
could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. "
What is the best practice for this query?