我只想构建一个动态过滤器。最后返回
Expression<Func<Event, bool>>
我尝试使用 Combine (AndAlso) 表达式,但它不起作用,最后我发现有 IQueryable 查询效果很好,但现在我怎样才能将它转换为返回类型 -
Expression<Func<Event, bool>>?
我的代码:
public IQueryable<Event> GetBySearch(EventFilter search)
{
IQueryable<Event> query = this.Context.Events.AsQueryable();
Expression<Func<Event, bool>> expression = null;
if (search.CategoryId != 0)
{
query = query.Where(x => x.CategoryId == search.CategoryId);
}
if (search.SubCategoryId != 0)
{
query = query.Where(x => x.SubCategoryId == search.SubCategoryId);
}
expression = query.Expression as Expression<Func<Event, bool>>; //This convert is not working, it returns null.
return this.Context.Events.Where(expression);
}