1

I have moved my application from dotnet 2.2 to 3.0. I've solved big and small problems, but the LINQ queries I wrote in the service layer are failing.

For example, this method (and linq query) not work:

public async Task<IList<PersonnelDto>> PersonnelChart(short year)
    {
        var result = await uow.Repository<Personnel>().Query()
            .Where(x => !x.IsOpen)
            .Include(i => i.CurrentMoney)
            .GroupBy(gp => new { id = gp.CurrencyType})
            .Select(s => new PersonnelChartDto
            {
                PersonnelId = s.Key.id,
                PersonnelCode = s.Key.code,
                PersonnelName = s.Key.name,
                PersonnelCount = s.Count(),
                SavingTotal = Math.Round(s.Sum(sm => sm.CurrentMoney
                   .Where(x => !x.IsDeleted)
                   .Select(ss => ss.SavingTotal)
                   .DefaultIfEmpty(0)
                   .Sum()), 2),                    
            })
            .ToListAsync();

        return result;
    }

When trigger this method, EF Core throw that error:

System.InvalidOperationException: Processing of the LINQ expression 'AsQueryable((Unhandled parameter: sm).CurrentMoney)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core.

So, how can I fix this error?

Samet Öz
  • 91
  • 1
  • 5
  • 2
    Does this answer your question? [EF Core 2.2 LINQ query not working in EF Core 3.0](https://stackoverflow.com/questions/58092869/ef-core-2-2-linq-query-not-working-in-ef-core-3-0) – phuzi Dec 03 '19 at 11:50
  • 2
    I suspect it's the `Math.Round` giving you trouble. Does it work without that? – DavidG Dec 03 '19 at 11:52
  • yes I check that before, but wont help. – Samet Öz Dec 03 '19 at 11:53
  • You have an expression tree. The exception very clearly says unhndled parameter sm. You can use the AsEnumerable() after the GroupBy(). Is this what you want though? This will be handled in memory not in sql. The limitation is that EF cant translate to SQL. – panoskarajohn Dec 03 '19 at 13:40
  • Math.Round won't translate. DefaultIfEmpty with argument is broken https://github.com/aspnet/EntityFrameworkCore/issues/17783 – joakimriedel Dec 09 '19 at 07:19
  • For me, even DefaultIfEmpty without argument is broken - it compiles into a query with syntax error. An inefficient workaround: `collection.Any() ? collection.Sum(ss => ss.SavingTotal) : 0` – wondra Jun 09 '20 at 16:02

0 Answers0