Question

I'm trying to select a list of integers and it's raising an exception.

Exception message: System.ArgumentException: Expression of type 'System.Collections.Generic.IAsyncEnumerable1[System.Int32]' cannot be used for parameter of type 'System.Collections.Generic.IAsyncEnumerable1[System.Object]' of method 'System.Collections.Generic.IAsyncEnumerable1[MyProject.Model.Entities.MyTable] CastModel' Parameter name: arg0

Steps to reproduce

Model

public class MyTable { 
  public int MyTableId { get; set; } 
  public int SomeKey { get; set; } 
  public int MyFieldIntegerIWant { get; set; } 
} 

Run the query below (or one like it):

int keyId;
var ids = await context.MyTable.AsNoTracking()
        .Where(x => x.SomeKey.Equals(keyId))
        .Select(x => x.MyFieldIntegerIWant)
        .ToListAsync();

Further technical details

EF Core version: 1.1.0

Database Provider: Microsoft.EntityFrameworkCore.SqlServer

Operating system: Windows 7

IDE: Visual Studio 2015

UPDATE:

The issue had something to do with EF Plus' QueryFilters https://github.com/zzzprojects/EntityFramework-Plus/issues/133

Expert Answer

After discussion with @thejason,

The issue was not caused by Entity Framework Plus but how EF Core handle cast.

The issue can be easily reproduced using the following code:

using (var ctx = new CurrentContext())
{
    int keyId = 1;

    var ids = ctx.MyTables
        .Cast<IMyTable>()
        .Cast<MyTable>()
        .Where(x => x.SomeKey.Equals(keyId))
        .Select(x => x.MyFieldIntegerIWant)
        .ToList();
}

Cast method will be fixed in EF Core 2.x




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why