vendredi 2 octobre 2020

.NET Create search expression dynamically [duplicate]

I am trying to build a simple search expression based on a user string input. Basically, I need to translate a property access expression into a Predicate based on String.Contains method

var data = <some IQueryable<Entity>>;

Func<string, bool> fn = item => item.Contains(searchModel.Search);

data = data.Where(x => fn(x.{PropertyName}));

I am getting an error "...could not be translated"

Note I need to build the search expression with this "pattern" because it will be used to compose more than one expression dynamically. As you can see below, it's my real implementation

Func<object, bool> fn = item => item
                .ToString()
                .Contains(searchModel.Search);

var criteriaExpressions = jobColumnConfigs
     .Select(x => x.Item.ComposeWith(fn).ToExpression());
var searchCriteria = PredicateBuilder.Or(criteriaExpressions);
           
baseQuery = baseQuery.Where(searchCriteria);

Any thoughts on this?

Thanks





Aucun commentaire:

Enregistrer un commentaire