Open gojanpaolo opened 4 years ago
Normal LINQ extension methods return a fresh instance of the IQueryable
every time, one that is created using the original Provider. This way, the special properties of the IQueryable
that EF creates are preserved after calling various extension methods.
However, for this library to allow you to use the extra functions Containing
, etc, only after you called Search
first, the Search
will not use the provided Provider to build a new IQueryable, instead it will use its own implementation. When calling ToListAsync
on that, EF will complain as the EF special properties are lost.
There are two options to mitigate this problem:
public static IQueryable<TSource> Apply<TSource, TProperty>(this QueryableSearchBase<TSource, TProperty> source) {
return source.Where(source.AsExpression());
}
public static IQueryable<TParent> Apply<TParent, TChild, TProperty>(this QueryableChildSearchBase<TParent, TChild, TProperty> source) {
return source.Where(source.AsExpression());
}
// Usage:
await ctx.Foo.Search(f => f.Bar).Containing("").Apply().ToListAsync();
We're getting an
InvalidOperationException
when used with ef core andToListAsync
e.g.