Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contains() throws Exception Expression of type 'System.Collections.Generic.List`1[System.Int32]' cannot be used for parameter... #16972

Closed
kccsf opened this issue Aug 5, 2019 · 3 comments · Fixed by #17058
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@kccsf
Copy link

kccsf commented Aug 5, 2019

Contains() is throwing an exception when used as follows:

var posts = await context.Post
    .Where(x => blogs.Contains(x.Blog))
    .ToListAsync();

Exception message: "Expression of type 'System.Collections.Generic.List1[System.Int32]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable1[System.Nullable1[System.Int32]]' of method 'Boolean Contains[Nullable1](System.Collections.Generic.IEnumerable1[System.Nullable1[System.Int32]], System.Nullable`1[System.Int32])' (Parameter 'arg0')"

Stack trace: System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)\r\n at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0, Expression arg1)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityEqualityRewritingExpressionVisitor.VisitContainsMethodCall(MethodCallExpression methodCallExpression)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityEqualityRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)\r\n at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityEqualityRewritingExpressionVisitor.RewriteAndVisitLambda(LambdaExpression lambda, EntityReferenceExpression source)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityEqualityRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)\r\n at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityEqualityRewritingExpressionVisitor.Rewrite(Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.QueryOptimizer.Visit(Expression query)\r\n at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)\r\n at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_01.<ExecuteAsync>b__0()\r\n at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetAsyncEnumerator(CancellationToken cancellationToken)\r\n at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable1.GetAsyncEnumerator()\r\n at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__1291.MoveNext()\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at ConsoleApp3.Program.

d__0.MoveNext()

Steps to reproduce

    public class Blog
    {
        public int Id { get; set; }

        public ICollection<Post> Posts { get; set; }
    }

    public class Post
    {
        public int Id { get; set; }

        public Blog Blog { get; set; }
    }

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blog { get; set; }

        public DbSet<Post> Post { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
    }

    public class Program
    {
        public static async Task Main()
        {
            using (var context = new BloggingContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var blog1 = new Blog()
                {
                    Posts = new List<Post>() { new Post(), new Post() }
                };

                context.Add(blog1);
                context.SaveChanges();
            }

            using (var context = new BloggingContext())
            {
                var blogs = await context.Blog.ToListAsync();

                var posts = await context.Post
                  .Where(x => blogs.Contains(x.Blog))
                  .ToListAsync();
            }
        }
    }

Further technical details

EF Core version: 3.0.0-preview9.19405.9
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.0 Preview 1.0

@ajcvickers
Copy link
Contributor

@roji Is this an entity equality issue?

@roji
Copy link
Member

roji commented Aug 8, 2019

Sorry for missing this, will look into it tomorrow.

@roji
Copy link
Member

roji commented Aug 9, 2019

Repro'd in latest, bug introduced in #16957. Fix on the way.

roji added a commit that referenced this issue Aug 9, 2019
roji added a commit that referenced this issue Aug 9, 2019
roji added a commit that referenced this issue Aug 9, 2019
@roji roji added type-bug closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Aug 9, 2019
@roji roji added this to the 3.0.0 milestone Aug 9, 2019
@roji roji self-assigned this Aug 9, 2019
roji added a commit that referenced this issue Aug 9, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview9 Aug 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview9, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants