How to apply Expression<Func> value selector in query?

31 Views Asked by At

I have such code:

private static IQueryable<Word> filterAndOrderWords(IQueryable<Word> words, string phrase)
{
  return words
    .Where(it => it.Phrase.ToLower().Contains(phrase))
               ...

which now I would like to make more generic and remove hard coded using Phrase property. So I could change it to:

private static IQueryable<T> filterAndOrderText<T>(IQueryable<T> textRecords,
  Expression< Func<T,string>> selector, string phrase)
{
  return textRecords
    .Where(it => selector(it).ToLower().Contains(phrase))
    ...        

But this does not compile. selector.Compile()(it) throws an exception (I used EF SQlite as source, and the message is I should rewrite the query).

So how to rewrite it so EF could understand the more generic version as well as the original one?

0

There are 0 best solutions below