Simple Linq Select not working


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


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)

Further technical details

EF Core version: 1.1.0

Database Provider: Microsoft.EntityFrameworkCore.SqlServer

Operating system: Windows 7

IDE: Visual Studio 2015


The issue had something to do with EF Plus' QueryFilters

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
        .Where(x => x.SomeKey.Equals(keyId))
        .Select(x => x.MyFieldIntegerIWant)

Cast method will be fixed in EF Core 2.x

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