I'm trying to use Expression.NotEqual() (here)
But I can't understand how to use the methodInfo.
I'm trying to make a comparison to a possible bool?.
This is the property that I want to evaluate, which was added to database after release (creating null's as well as true and false:
public bool? Deactivated { get; set; } = false;
I'm using a working expression builder with generic that accepts parameters sent in a object that has a List of WhereParameters like below:
List<WhereParameters> whereList = new List<WhereParameters>();
whereList.Add(new WhereParameters
{
field = "ID",
field_Value_Relation = "notEqual",
value = Guid.Empty
});
whereList.Add(new WhereParameters
{
field = "Deactivated",
field_Value_Relation = "notEqualorNull", //orNull => nullable
value = true
});
These parameters are used in the builder:
public List<M> getExpression(QueryParameters<M> QueryParams)
{
//GET TYPE FROM GENERIC M
Type modelType = QueryParams.modelType;
//QUERY DB with GENERIC TYPE
IQueryable<M> queryableData = (IQueryable<M>)QueryParams.db.Set(modelType);
// Compose the expression tree that represents the parameter to the predicate.
//CREATE EXPRESSION PARAMETER
ParameterExpression pe = Expression.Parameter(typeof(M), "param");
// Create an expression tree that represents the expression
//if (QueryParams.whereParameters != null && QueryParams.whereParameters.Count > 0)
//{
int w = 0;
foreach (WhereParameters whereParam in QueryParams.whereParameters) {
//BLOCK A.1 WHERE
//Create Property on the Left of equality
Expression left1 = Expression.Property(pe, whereParam.field.ToLower());
//Create Property on the right of equality
Expression right1 = Expression.Constant(whereParam.value);
//Expresses the relation between left and right
//Expression e1;
switch (whereParam.field_Value_Relation)
{
case "notEqualOrNull":
predicateBody = Expression.NotEqual(left1, right1, true , ????????);
break;
}
}
It is throwing a System.InvalidOperationException: The binary operator NotEqual is not defined for the types 'System.Nullable`1[System.Boolean]' and 'System.Boolean'.
I don't know how to make this work with nullables. I understood the islifted param, but can't manage to understand what to send in the last parameter of Expression.NotEqual, as MethodInfo. Can someone please show me examples on how this can be done?