IComparable/IEquatable could not be translated
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
I recently ran into a bit of unexpected behavior where casting to `IComparable` or `IEquatable<>` causes a previously working query to become untranslatable. It's especially unexpected that casting to `object` still works but `IComparable` does not.
I have restructured my code to work around this quirk but wanted to put it out there since I couldn't find an existing issue for it.
### Minimal reproducible example
```C#
using Microsoft.EntityFrameworkCore;
using (var db = new MyContext())
{
db.MyEntities.OrderBy(x => x.Id).ToList(); // works
db.MyEntities.OrderBy(x => (object)x.Id).ToList(); // works
db.MyEntities.OrderBy(x => (IComparable)x.Id).ToList(); // breaks
db.MyEntities.OrderBy(x => (IComparable)x.Id).ToList(); // breaks
db.MyEntities.OrderBy(x => (IEquatable)x.Id).ToList(); // breaks
}
class MyContext : DbContext
{
public DbSet MyEntities { get; set; }
}
class MyEntity
{
public int Id { get; set; }
}
```
### Stack trace
```
System.InvalidOperationException
HResult=0x80131509
Message=The LINQ expression 'DbSet()
.OrderBy(m => (IComparable)m.Id)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.$(String[] args)
```
### Include provider and version information
EF Core version: 7.0.2 & 6.0.13
Database provider: Microsoft.EntityFrameworkCore.SqlServer & Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 6.0 & .NET 7.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.4.4
Contributor guide
Assessment
This issue has not been assessed yet.