Build lambda expressions with contains

  • Thread starter Thread starter Henk A. Sandoval
  • Start date Start date
H

Henk A. Sandoval

Guest
Good affertoon people.

I have a problem, when I tried build a lambda expression with Expression.Builder the code throw a error.

Say: "Static method requires null instance, non-static method requires non-null instance.".

I'm already successful with simple expressions like:


IQueryable<PersonTestModel> expected = data.Where(x => x.IsActive);

PersonTestModel expected = data.FirstOrDefault(x => x.Id != 3);

IQueryable<PersonTestModel> expected = data.Where(x => x.FirstName.StartsWith("a", StringComparison.InvariantCultureIgnoreCase));

IQueryable<PersonTestModel> expected = data.Where(x => x.Balance > averageBalance);

but, I can't build this expression

var dataFilter = new HashSet<int> { 2 };
IQueryable<PersonTestModel> data = await PersonFaker.GetFakeSimpleData(60);

//this is expression i will try
Expression<Func<PersonTestModel, bool>> func = x => dataFilter.Contains(x.UserId);
var expected = data.Where(func);
This is my code, for build commos expressions:

private static Expression GetExpression<T>(ParameterExpression parameter, QueryFilter queryFilter)
{
MemberExpression member = Expression.Property(parameter, queryFilter.PropertyName);
ConstantExpression constant = GetConstant(member.Type, queryFilter.Value);

switch (queryFilter.Operator)
{
case Operator.Equals:
return Expression.Equal(member, constant);
case Operator.NotEqual:
return Expression.NotEqual(member, constant);
case Operator.Contains:
case Operator.StartsWith:
case Operator.EndsWith:
{
constant = GetConstant(member.Type, queryFilter.Value.ToUpper());
return Expression.Call(Expression.Call(member, "ToUpper", null), queryFilter.Operator.ToString(), null, constant);
}
case Operator.GreaterThan:
return Expression.GreaterThan(member, constant);
case Operator.GreaterOrEqualThan:
return Expression.GreaterThanOrEqual(member, constant);
case Operator.LessThan:
return Expression.LessThan(member, constant);
case Operator.LessOrEqualThan:
return Expression.LessThanOrEqual(member, constant);
default:
throw new Exception($"The type {queryFilter.Operator} not is a type selector valid.");
}
}
but with that expression I received a error message, this is the code

private static Expression GetComplexExpression<T>(ParameterExpression parameter, QueryFilter queryFilter)
{
MemberExpression member = Expression.PropertyOrField(parameter, queryFilter.PropertyName);

MethodInfo method = typeof(HashSet<int>).GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Single(x => x.Name == "Contains" && x.IsFinal && x.GetParameters().Length == 1);
var constant = Expression.Constant(queryFilter.Values);
MethodCallExpression methodCallExpression = Expression.Call(method, constant, member);

return methodCallExpression;
}

I search on google, but the only result on my searchs is this code on stackoverflow:

Build Lambda Expressions with Contains
Very thanks for you interesting...

Continue reading...
 
Back
Top