zoukankan      html  css  js  c++  java
  • C# DataTable 使用linq 动态拼接查询

    //动态拼接多个参数,可以更加条件重新更改
    //typeof(string)是数据类型
    private static Func<DataRow, bool> ExoerssionCondition(DataRow dr, List<CompareConfig> columnList)
            {
                ParameterExpression r = Expression.Parameter(typeof(DataRow), "r"); //DataRow参数:r
                Expression con = Expression.Constant(true); //All nested conditions
                foreach (var item in columnList)
                { 
                    ConstantExpression expFieldName = Expression.Constant(item.TargetField, typeof(string)); //"Height" 字符串常量
                    ConstantExpression expValue= Expression.Constant(dr[item.SourceField].ToString(), typeof(string));//创建float常量5.0 }
                    List<Expression> list = new List<Expression>();
                    list.Add(r); list.Add(expFieldName); //参数列表:DataRow, "Height"
                    MethodInfo mi = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow), typeof(string) }).MakeGenericMethod(typeof(string));
                    MethodCallExpression MC = Expression.Call(null, mi, list); //创建MethodCallExpression
    
                    Expression SingleExpCon = Expression.Equal(MC, expValue);
                    con = Expression.And(con, SingleExpCon); 
                } 
                var expLam = Expression.Lambda<Func<DataRow, bool>>(con, r);
                return expLam.Compile();  
            }
    

      

    参考例子:https://blog.csdn.net/alai7150/article/details/103086231

  • 相关阅读:
    Docker安装及简单使用
    常用编程语言注释符
    常用正则标记
    Android studio 使用startService报错:IllegalStateException
    Mybatis映射文件中#取值时指定参数相关规则
    IDEA Maven项目的Mybatis逆向工程
    循环结构
    每日思考(2020/03/05)
    分支结构
    每日思考(2020/03/04)
  • 原文地址:https://www.cnblogs.com/jerrywublogs/p/14307481.html
Copyright © 2011-2022 走看看