Skip to content

ArgumentException: The given expression '([o] As Bar)' does not contain the searched expression '[o]'  #12741

Closed

Description

@myrup

Getting an ArgumentException when attempting to list contents of a DbSet as a subclassed List.

Exception message: 

ArgumentException: The given expression '([o] As Bar)' does not contain the searched expression '[o]' in a nested NewExpression with member assignments or a MemberBindingExpression.
Parameter name: fullExpression

Stack trace:

System.RuntimeMethodHandle.InvokeMethod(object target, object[] arguments, Signature sig, bool constructor, bool wrapExceptions)
System.Reflection.RuntimeMethodInfo.Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.TrackEntitiesInResults<TResult>(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor<TResult>(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery<TResult>(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore<TResult>(Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger<Query> logger, Type contextType)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass13_0<TResult>.<Execute>b__0()
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<TFunc>(object cacheKey, Func<Func<QueryContext, TFunc>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery<TResult>(object cacheKey, Func<Func<QueryContext, TResult>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
Remotion.Linq.QueryableBase<T>.GetEnumerator()
System.Collections.Generic.List<T>.AddEnumerable(IEnumerable<T> enumerable)
System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)

Steps to reproduce

class Foo : Bar {
}
class Bar {
}

context.Foos.Select(b => b as Bar).ToList();

(context is a DbContext with a DbSet<Foo> Fooson it)

Further technical details

EF Core version: 2.1.1
Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL
Operating system: OS X 10.13.5
IDE: Visual Studio Community 7.5.3

Activity

  1. myrup commented on Jul 20, 2018

    @myrup
    Author

    Forgot to mention context.Foos.ToList().Select(b => b as Bar).ToList(); gives the desired result.

  2. added this to the milestone on Jul 20, 2018
  3. maumar commented on Aug 17, 2018

    @maumar
    Contributor

    Full repro:

        class Program
        {
            static void Main(string[] args)
            {
                using (var ctx = new MyContext())
                {
                    ctx.Database.EnsureDeleted();
                    ctx.Database.EnsureCreated();
    
                    var f = new Foo();
                    ctx.Foos.AddRange(f);
                    ctx.SaveChanges();
                }
    
                using (var ctx = new MyContext())
                {
                    var query1 = ctx.Foos.Select(b => (Bar)b).ToList(); // this works
                    var query2 = ctx.Foos.Select(b => b as Bar).ToList(); // this fails
    
                }
            }
        }
    
        public class MyContext : DbContext
        {
            public DbSet<Foo> Foos { get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Bar>();
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(@"Server=.;Database=Repro12741;Trusted_Connection=True;MultipleActiveResultSets=True");
            }
        }
    
        public class Foo : Bar
        {
        }
        public class Bar
        {
            public int Id { get; set; }
        }
  4. maumar commented on Aug 17, 2018

    @maumar
    Contributor

    This is because relinq's AccessorFindingExpressionVisitor doesn't properly handle TypAs expression. Workaround is to use hard cast instead (shown in repro code above).

    Filed a bug on relinq to track this: https://www.re-motion.org/jira/browse/RMLNQ-124

  5. removed this from the milestone on Aug 17, 2018
  6. added this to the 3.0.0 milestone on Aug 20, 2018
  7. modified the milestones: 3.0.0, Backlog on Jun 28, 2019
  8. 2 remaining items

  9. modified the milestones: Backlog, on Sep 4, 2019
  10. maumar commented on Sep 11, 2019

    @maumar
    Contributor

    verified this issue has been fixed in 3.0

  11. modified the milestones: , 3.0.0 on Sep 11, 2019
  12. added 2 commits that reference this issue on Sep 12, 2019
    6443c98
    36a7bdf
  13. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions