• linq to sql 动态构建查询表达式树


    通过Expression类进行动态构造lamda表达式。

    实现了以下几种类型,好了代码说话:

    public Expression<Func<T, bool>> GetAndLambdaExpression<T>(string[] keys,object[] values,string[] methons)
    {

    Expression expression_return = Expression.Constant(true);
    Type TType = typeof(T);
    ParameterExpression expression_param = Expression.Parameter(TType, "p");
    Expression temp;
    for (int i = 0; i < keys.Length; i++)
    {
        switch (methons[i])
        {
            case "=":
                temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                    TType.GetMethod("ToString")),
                 Expression.Constant(values[i]));
                expression_return = Expression.And(expression_return, temp);
                break;
            case "%":
                temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                    typeof(string).GetMethod("Contains"),
                    Expression.Constant(values[i], typeof(string)));
                expression_return = Expression.And(expression_return, temp);
                break;
            case ">":
                //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                //    TType.GetProperty(keys[i]).GetType().GetMethod("GreaterThan"), Expression.Constant(values[i]));
                temp = Expression.GreaterThan(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i])));
                expression_return = Expression.And(expression_return, temp);
                break;
            case "<":
                //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                //   Expression.GetType().GetMethod("LessThan"), Expression.Constant(values[i]));
                temp=Expression.LessThan(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i])));
                expression_return = Expression.And(expression_return, temp);
                break;
            case ">=":
                //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                //    TType.GetProperty(keys[i]).GetType().GetMethod("GreaterThanOrEqual"), Expression.Constant(values[i]));
                temp = Expression.GreaterThanOrEqual(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i])));
                expression_return = Expression.And(expression_return, temp);
                break;
            case "<=":
                //temp = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                //    TType.GetProperty(keys[i]).GetType().GetMethod("LessThanOrEqual"), Expression.Constant(values[i]));
                temp = Expression.LessThanOrEqual(Expression.Property(expression_param, TType.GetProperty(keys[i])), Expression.Constant(Convert.ToInt32(values[i])));
                expression_return = Expression.And(expression_return, temp);
                break;
            case "in":
                string[] strarr = values[i].ToString().Split(',');
                Expression or_return = Expression.Constant(false);
                for (int k = 0; k < strarr.Length; k++)
                {
                    temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                        TType.GetMethod("ToString")),
                     Expression.Constant(strarr[k]));
                    or_return = Expression.Or(or_return, temp);
                }
     
                expression_return = Expression.And(expression_return, or_return);
                break;
        }
    }
     
    return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
    

    }

           IList<People> peopleList = new List<People>();
                for (int i = 1; i <= 6; i++)
                {
                    People people = new People() { Name = "Dai_" + i.ToString(), Age = 16, Birthday = DateTime.Now, IsBuyCar = true, Sex = "F" };
                    peopleList.Add(people);
                }
                Expression<Func<People, bool>> func_a = GetAndLambdaExpression<People>(new string[] { "Name", "Age" }, new string[] { "Dai_2", "16" }, new string[] { "=", "<" });
                var people_data = peopleList.AsEnumerable().Where(func_a.Compile()).ToList();
  • 相关阅读:
    软件工程第八周
    e课表项目第二次冲刺周期第十天
    蓝桥杯java历年真题及答案整理41~56
    蓝桥杯java历年真题及答案整理21~40
    蓝桥杯java历年真题及答案整理1~20.md
    异序二分查找 二分查找方程根 二分查找重复元素最后一个
    软件说明
    遗传算法--句子匹配
    Matlab学习笔记
    使用TenforFlow 搭建BP神经网络拟合二次函数
  • 原文地址:https://www.cnblogs.com/sjqq/p/8427788.html
Copyright © 2020-2023  润新知