I have a method like this:
protected async Task<bool> IsExist(Category entity)
{
return await _context.Cateogries.AnyAsync(s => s.Integrated == entity.Integrated);
}
I have many entities that have a property Integrated
. I want to create a generic method to match for all types.
protected async Task<bool> IsExist(Func<TEntity, bool> property, bool value)
{
return await _context.Set<TEntity> AnyAsync(s => property(s) == value);
}
And using it like this:
IsExist(s => s.Integrated, true)
But it does not work:
InvalidOperationException: The LINQ expression 'DbSet() .Any(y => Invoke(__property_0, y) .Value == __value_1)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
question from:
https://stackoverflow.com/questions/65848922/how-to-implement-entity-framework-genetic-property-match-for-query 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…