我想把我的IQueryable转换成一个接口,类似于这样:
它可以在EF Core 2.2上运行,但在3上会出现此错误:
public static IQueryable<T> _enableFilter<T>(this IQueryable<T> queryable) => queryable.Where(x => (x as IEnable).Enable);
_newsRepository.BaseQuery.EnableFilter().FirstOrDefaultAsync(x => x.Id == model.Id);
它可以在EF Core 2.2上运行,但在3上会出现此错误:
System.InvalidOperationException : The LINQ expression 'Where<News>(
source: DbSet<News>,
predicate: (n) => (n as IEnable).Enable)' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
IEnable
派生类上工作的通用方法,请添加类型约束,即> _enableFilter<T> where T:IEnable
。 - Panagiotis Kanavos