我正在尝试从ODataQueryOptions
中提取过滤器表达式,以便我可以在我的业务逻辑类中使用它。
public PageResult<Poco> Get(ODataQueryOptions odataQueryOptions)
{
Expression<Func<Poco, bool>> myExpression = ... // what do i do here?
var result = _myBusinessLogic.Search(myExpression);
return new PageResult<Poco>(result, null, null);
}
我看了一下描述将查询翻译成HQL here的博客,我认为(至少我希望)这对我正在尝试做的事情来说太过分了。
我基本上需要以Expression<Func<Poco, bool>>
形式获取过滤器表达式。我尝试过玩ApplyTo()
,但我无法理解。任何帮助表示赞赏。
答案 0 :(得分:5)
我们有一个适合您需求的FilterBinder类,但遗憾的是内部类。不过你可以做一个简单的技巧来掌握$ filter表达式,
public static class ODataQueryOptionsExtensions
{
public static Expression ToExpression<TElement>(this FilterQueryOption filter)
{
IQueryable queryable = Enumerable.Empty<TElement>().AsQueryable();
queryable = filter.ApplyTo(queryable, new ODataQuerySettings());
return queryable.Expression;
}
}
在你的情况下,你可以这样做,
public PageResult<Poco> Get(ODataQueryOptions odataQueryOptions)
{
Expression<Func<Poco, bool>> myExpression = odataQueryOptions.Filter.ToExpression<Poco>();
var result = _myBusinessLogic.Search(myExpression);
return new PageResult<Poco>(result, null, null);
}
请注意,表达式包含的内容更像是这样,
SOTests.Customer[].Where($it => conditional-expression)
。因此,您可能必须从lambda中提取该条件表达式。