I'm curious about the following:
static readonly ICollection<T>
& static readonly IList<T>
field into a query when using it inside of a .Contains()
? When removing the static
keyword it seems to execute the query just fine?IEnumerable<T>
This is not an issue, is this because of the different .Contains()
that's being used?I got this from the exception: https://learn.microsoft.com/en-us/ef/core/querying/client-eval but that doesn't really explain to me why this is happening only when using a static collection.
Let's say I have this database model when using EF Core:
public class Item
{
public string Bar { get; set; }
public string Foo { get; set; }
}
And in my class I have the following field (this can be a static readonly IList<T>
or static readonly ICollection<T>
):
private static readonly IList<string> AllowedFooCodes = new List<string>() { "ABC", "CED" };
My db class looks something like this:
public class SampleDataRepository : ISampleDataRepository
{
private readonly ILogger<SampleDataRepository> _logger;
private readonly SampleDataStore _sampleDataStore;
// Can be IList or ICollection, exception will be thrown regardless.
private static readonly IList<string> AllowedFooCodes = new List<string>() { "ABC", "CED" };
public SampleDataRepository(SampleDataStore store, ILogger<SampleDataRepository> logger)
{
_sampleDataStore = store;
_logger = logger;
}
public async Task<IEnumerable<Item>> GetItems()
{
_sampleDataStore.Items
.AsNoTracking()
.Where(item => AllowedFooCodes.Contains(item.foo))
.ToListAsync();
}
}
I'm looking forward to your answers.
Seems like it was indeed a bug, looks like a Pull Request is already out for it as well: https://github.com/dotnet/efcore/pull/35029