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

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

Closed
myrup opened this issue Jul 20, 2018 · 4 comments · Fixed by #17805
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

@myrup
Copy link

myrup commented Jul 20, 2018

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

@myrup
Copy link
Author

myrup commented Jul 20, 2018

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

@divega divega added this to the 2.2.0 milestone Jul 20, 2018
@maumar
Copy link
Contributor

maumar commented Aug 17, 2018

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; }
    }

@maumar
Copy link
Contributor

maumar commented Aug 17, 2018

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

@maumar maumar removed this from the 2.2.0 milestone Aug 17, 2018
@ajcvickers ajcvickers added this to the 3.0.0 milestone Aug 20, 2018
@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 28, 2019
@AndriySvyryd AndriySvyryd added verify-fixed This issue is likely fixed in new query pipeline. and removed relinq-dependent labels Aug 22, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 3.1.0 Sep 4, 2019
@maumar
Copy link
Contributor

maumar commented Sep 11, 2019

verified this issue has been fixed in 3.0

@maumar maumar added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed punted-for-3.0 verify-fixed This issue is likely fixed in new query pipeline. labels Sep 11, 2019
@maumar maumar modified the milestones: 3.1.0, 3.0.0 Sep 11, 2019
maumar added a commit that referenced this issue Sep 12, 2019
Resolves #8723
Resolves #9241
Resolves #10172
Resolves #10210
Resolves #10548
Resolves #11847
Resolves #11933
Resolves #12741
Resolves #15798
@maumar maumar closed this as completed in 36a7bdf Sep 12, 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.

5 participants