简体   繁体   English

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

[英]Error in Create Dynamic Linq Query Expression

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

i get below error At => if (property.GetValue(this, null) != null)我得到以下错误 => if (property.GetValue(this, null) != null)

base {"Object does not match target type."} System.ApplicationException {System.Reflection.TargetException} base {"对象与目标类型不匹配。"} System.ApplicationException {System.Reflection.TargetException}

我解决了:如果(property.GetValue(Model,null)!= null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM