Aggregation with GROUP BY using AspNet Core OData and Entity Framework Core c# entity-framework-core linq2db odata


Simple OData aggregation fails on Entity Framework Core (SQL Sever) and Asp.Net Core:


Due to inability to translate used GroupBy LINQ expression warning is issued:

Microsoft.EntityFrameworkCore.Query: Warning: The LINQ expression 'GroupBy(new GroupByWrapper() {GroupByContainer = new LastInChain() {Name = "FileType", Value = [$it].FileType}}, [$it])' could not be translated and will be evaluated locally.

And then exception is thrown: operation could destabilize the runtime

most relevant stack:

at lambda_method(Closure , File )
   at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.GroupedEnumerable`3.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

Used NuGet packages versions:

  • Microsoft.AspNetCore.OData 7.0.1
  • Microsoft.EntityFrameworkCore.SqlServer 2.1.1
  • Microsoft.AspNetCore 2.1.2

Repo showing the issue:

Issue on GitHub:

I'm not sure where the issue lies, whether EF Core should be able to convert LINQ query to valid SQL or OData generates difficult to convert GroupBy expression. Until it's sorted out I'm looking for some workaround.

8/19/2018 1:20:16 PM

Popular Answer

Use Linq2Db extensions to EF Core as workaround.

After adding NuGet packages:

  • linq2db 2.2.0
  • linq2db.EntityFrameworkCore 1.0.1

And changing from

    public IActionResult Get()
        return Ok(_db.Files);


    public IActionResult Get()
        return Ok(_db.Files.ToLinqToDB());

everything works.

Branch with fix: linq2db.EntityFrameworkCore :

EDIT: sum, avg, min, max works, countdistinct does not work

8/19/2018 3:35:48 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow