Applying an Expression<> to a LINQ Where clause

淺唱寂寞╮ 提交于 2020-07-23 06:37:34

问题


I'm writing a custom Entity Framework filter. I have a list of IDs, and a user-supplied expression.

protected IEnumerable<TColumn> FilterIds;
protected Expression<Func<T, IEnumerable<TColumn>, bool>> Filter;

Now my question is how do I apply the Filter expression to a Where() clause?

public virtual IQueryable<T> ApplyFilter(IQueryable<T> query)
{
    if (FilterMode == FilterModeMatchAny && HasFilterIds)
    {
        // Whoops! Can't do this!
        return query.Where(x => Filter(x, FilterIds));
    }
    return query;

回答1:


Using the Combine method from this post does pretty much everything you need. From there you just need to turn the literal value into an expression that computes it (or I guess alter the Combine method from that answer so that the intermediate value isn't computed from a lambda but rather is just any expression), and then call the function.

protected IEnumerable<TColumn> FilterIds;
protected Expression<Func<T, IEnumerable<TColumn>> FilterIdsExpression => _ => FilterIds;
protected Expression<Func<T, IEnumerable<TColumn>, bool>> Filter;

public virtual IQueryable<T> ApplyFilter(IQueryable<T> query)
{
    if (FilterMode == FilterModeMatchAny && HasFilterIds)
    {
        return query.Where(FilterIdsExpression.Combine(Filter));
    }
    return query;
}



回答2:


You can create a new expression to invoke the Filter using the FilterIds and use Expand from LinqKit

Edit Thanks to @Servy's comment. Now I expand only the internal expression instead of entire query.

...
protected IEnumerable<TColumn> FilterIds;
protected Expression<Func<T, IEnumerable<TColumn>, bool>> Filter;

public virtual IQueryable<T> ApplyFilter(IQueryable<T> query)
{
    if (FilterMode == FilterModeMatchAny && HasFilterIds)
    {
        Expression<Func<T, bool>> expression = x => Filter.Invoke(x, FilterIds);
        return query.Where(expression.Expand());
    }
    return query;
}
...


来源:https://stackoverflow.com/questions/62879661/applying-an-expression-to-a-linq-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!