创建动态 Linq 查询表达式时出错

本文关键字:表达式 出错 查询表 查询 动态 Linq 创建 | 更新日期: 2023-09-27 18:21:14

    PropertyInfo[] All_prop = Get_All_prop2(Model);
    //------Get Properties has Value-----
    foreach (PropertyInfo property in All_prop)
    {
        //--Check Has Value Property
        if (property.GetValue(this, null) != null)
        {
            IQueryable<DAL_BankWebApp.TBUSER> Query = cntx.TBUSERs.Select(x => x);
            //--Create Expression Of Property
            ParameterExpression pe = Expression.Parameter(typeof(string), property.Name);
            ConstantExpression Constant = Expression.Constant(property.GetConstantValue());
            Expression Contain = Expression.Call(pe, typeof(string).GetMethod("Contains", new[] { typeof(string) }), Constant);
            //----Create Where
            MethodCallExpression whereCallExpression = Expression.Call(
                typeof(Queryable),
                "Where",
                new Type[] { Query.ElementType },
                Query.Expression,
                Expression.Lambda<Func<string, bool>>(Contain, new ParameterExpression[] { pe }));
            IQueryable<DAL_BankWebApp.TBUSER> results = Query.Provider.CreateQuery<DAL_BankWebApp.TBUSER>(whereCallExpression);
            Result = results;
        }
    }

我得到下面的错误 => 如果(属性。GetValue(this, null( != null(

base {"对象与目标类型不匹配。System.ApplicationException {System.Reflection.TargetException}

创建动态 Linq 查询表达式时出错

我解决了:如果(属性。GetValue(Model, null( != null(