如何在C#的表达式相等中使用MethodInfo和lifttonull [英] How to use MethodInfo and lifttonull in Expression equality on c#

查看:46
本文介绍了如何在C#的表达式相等中使用MethodInfo和lifttonull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Expression.NotEqual()(

I'm trying to use Expression.NotEqual() (here)

但是我不明白如何使用methodInfo.

But I can't understand how to use the methodInfo.

我正试图与可能的布尔值进行比较?.

I'm trying to make a comparition to a possible bool?.

这是我要评估的属性,它在发布后添加到数据库中(创建null以及true和false:

This is the property that I want to evaluate, wich was added to database after release (creating null's as well as true and falses:

public bool? Deactivated { get; set; } = false;

我正在使用具有通用性的工作表达式生成器,该生成器可以接受在对象中发送的参数,该对象具有诸如Bellow之类的WhereParameters列表:

I'm using a working expression builder with generic that accepts parameters sent in a object that has a List of WhereParameters like bellow:

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;
                }
}

它抛出System.InvalidOperationException:未为类型'System.Nullable`1 [System.Boolean]'和'System.Boolean'定义二进制运算符NotEqual.

It is throwing a System.InvalidOperationException: The binary operator NotEqual is not defined for the types 'System.Nullable`1[System.Boolean]' and 'System.Boolean'.

我不知道如何使用nullable来实现此目的.我理解了被提升的参数,但是无法理解要在Expression.NotEqual的最后一个参数中作为MethodInfo发送什么.有人可以给我看看如何做到这一点的例子吗?

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?

谢谢大家

推荐答案

遇到了一个答案重构之前我的调试代码(保留所有内容,仅更改Switch,大小写):

My debug code before refactoring (maintain everything, just changing the Switch, case):

                 case "notEqualOrNull":
             
                       //this block will be promoted out of here
                        var field = whereParam.field;
                        var fieldValue = whereParam.value;
                        var prop = modelType.GetProperty(field);
                        var isNullable = prop.PropertyType.IsNullableType();
                       
                        var member = Expression.Property(pe, field);


                        var filter1 =
                        Expression.Constant(Convert.ChangeType(fieldValue, member.Type.GetGenericArguments()[0]));
                        Expression typeFilter = Expression.Convert(filter1, member.Type);
                        
                        //this is a proxy of the code that will be here after refactoring
                        andPredicateBody = Expression.NotEqual(left1, typeFilter);
                        predicateBody = Expression.OrElse(predicateBody, andPredicateBody);

                        break;

一旦可以测试所有可为空的变量,为了更通用,将对其进行重构.唯一的变化是var right1或typefilter.

This will be refactored, in order to be more general, once it can test all nullables. the only change in the 'switch case' will be the var right1 or typefilter.

这篇关于如何在C#的表达式相等中使用MethodInfo和lifttonull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆