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

Error querying filtered entities OfType<T> #14392

Closed
Jagbri opened this issue Jan 11, 2019 · 2 comments
Closed

Error querying filtered entities OfType<T> #14392

Jagbri opened this issue Jan 11, 2019 · 2 comments

Comments

@Jagbri
Copy link

Jagbri commented Jan 11, 2019

I have tried this code and throws System.InvalidOperationException on result2 variable.

  class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new TestContext())
            {
                if (ctx.Database.EnsureCreated())
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ctx.Add(new Entity());
                    }

                    ctx.SaveChanges();
                }

                var result = ctx.Entities.OfType<IEntity>().ToArray().Select(e => e.Id); //Works
                var result2 = ctx.Entities.OfType<IEntity>().Select(e => e.Id).ToArray(); //Fails
            }
        }
    }

    public class TestContext : DbContext
    {
        public DbSet<Entity> Entities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=test.db");
        }
    }

    public class Entity : IEntity
    {
        public int Id { get; set; }
    }

    public interface IEntity
    {
        int Id { get; set; }
    }

Error Detail

System.InvalidOperationException
HResult=0x80131509
Message=The query model's result type cannot be changed to 'System.Linq.IQueryable1[System.Int32]'. The result type may only be overridden and set to values compatible with the ResultOperators' current data type ('System.Linq.IQueryable1[TestQuery.IEntity]').
Source=Remotion.Linq

StackTrace

at Remotion.Linq.QueryModel.GetOutputDataInfo()
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.IsTrackingQuery(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel, Boolean asyncQuery)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger1 logger, Type contextType) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_01.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Remotion.Linq.QueryableBase1.GetEnumerator()
at System.Collections.Generic.LargeArrayBuilder1.AddRange(IEnumerable1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at TestQuery.Program.Main(String[] args) in C:\Users\User\Projects\TestQuery\Program.cs:line 23

Inner Exception 1:
ArgumentException: 'System.Linq.IQueryable`1[System.Int32]' cannot be used as the data type for a sequence with an ItemExpression of type 'TestQuery.IEntity'.
Arg_ParamName_Name

Further technical details

EF Core version: 2.2.1
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Frameworks: .Net Core 2.2 and .Net Framework 4.7.2
Operating system: Windows 10 Pro Version 10.0.17763 Build 17763
IDE: Visual Studio 2017 15.9.5

@smitpatel
Copy link
Member

OfType on interface type is client eval.

@ajcvickers
Copy link
Member

Duplicate of interface mapping: #13344

@smitpatel smitpatel removed their assignment Jan 12, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants