dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.75k stars 3.18k forks source link

List.Contains does not work with List<object> #20624

Closed steffibreuer closed 4 years ago

steffibreuer commented 4 years ago

Hello,

i'd like to create a query which filters by a List<object> in EFCore 3.1.3. This was working before in EFCore 2.2.6.

Steps to reproduce

Filtering by a List<object> throws an InvalidOperationException.

var itemIds = new List<object> { Guid.NewGuid() };
var items = await context.Item
    .Where(p => itemIds.Contains(p.ItemId))
    .ToListAsync();

System.InvalidOperationException: Null TypeMapping in Sql Tree Stack Trace: SqlTypeMappingVerifyingExpressionVisitor.VisitExtension(Expression node) Expression.Accept(ExpressionVisitor visitor) ExpressionVisitor.Visit(Expression node) InExpression.VisitChildren(ExpressionVisitor visitor) ExpressionVisitor.VisitExtension(Expression node) SqlTypeMappingVerifyingExpressionVisitor.VisitExtension(Expression node) Expression.Accept(ExpressionVisitor visitor) ExpressionVisitor.Visit(Expression node) RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression) RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression) RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression) RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate) QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) MethodCallExpression.Accept(ExpressionVisitor visitor) ExpressionVisitor.Visit(Expression node) QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) Database.CompileQuery[TResult](Expression query, Boolean async) QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) <>c__DisplayClass12_01.<ExecuteAsync>b__0() CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken) EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken) EntityQueryable1.GetAsyncEnumerator(CancellationToken cancellationToken) ConfiguredCancelableAsyncEnumerable1.GetAsyncEnumerator() EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken)

When i change the List type to Guid it works. As this is only a simplified sample code changing the List type is not a prefereable option.

var referencedIds = new List<Guid> { Guid.NewGuid() };
var items = await context.Item
    .Where(p => itemIds.Contains(p.ItemId))
    .ToListAsync();

Further technical details

EF Core version: 3.1.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: netcoreapp3.1 Operating system: Windows 10 IDE: Visual Studio 2019 16.4

LouLeGrain commented 4 months ago

Hello, i encountered the exact same error, in the same context with EF Core 8.0.6.

I made a small repo for reproduction here

We found a workaround to type the list we were using with reflection, but i thought I'll let you know.

ajcvickers commented 4 months ago

@LouLeGrain I filed #33928 to track this.