0

I would like to leverage LinqKit's AsExpandable feature to be able to translate my custom methods inside of my linq query.

I can achieve this by implementing my own expression visitor which will translate my custom methods to expression that EF provider can understand.

After digging a bit into the LinqKit source code, it seems to me that LinqKit was not designed to support such scenario even if it says so on its home page (see capture).

How can one plug a custom visitor into the LinqKit's pipeline ? Any ideas or advices ?

Thanks a lot !

Riana

From LinqKit home page

Riana
  • 689
  • 6
  • 22

1 Answers1

2

I think they are just referring to this overload of AsExpandable:

public static IQueryable<T> AsExpandable<T>(this IQueryable<T> query, Func<Expression, Expression> queryOptimizer)

Although the parameter is named "queryOptimizer", you can really pass in any Func<Expression, Expression> and that will be invoked immediately before the Expression is passed into the query provider:

    IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)
    {
        var expanded = expression.Expand();
        var optimized = _queryOptimizer(expanded);
        return _query.InnerQuery.Provider.CreateQuery<TElement>(optimized).AsExpandable();
    }

By default, _queryOptimizer just ends up being an ID function. It's named as such because LinqKit is also affiliated with this project: https://github.com/Thorium/Linq.Expression.Optimizer.