zoukankan      html  css  js  c++  java
  • C#实现通用数据过滤窗体

    /// <summary>
    /// 获取查询表达式树 (zuowenjun.cn)
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="fieldName"></param>
    /// <param name="operatorName"></param>
    /// <param name="value"></param>
    /// <param name="value2"></param>
    /// <returns></returns>
    public static Expression<Func<TEntity, bool>> GetQueryExpression<TEntity>(string fieldName, string operatorName, string value, string value2) where TEntity : class
    {
        PropertyInfo fieldInfo = typeof(TEntity).GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
        Type pType = fieldInfo.PropertyType;
        if (string.IsNullOrEmpty(operatorName))
        {
            throw new ArgumentException("运算符不能为空!""operatorName");
        }
        dynamic convertedValue;
        if (!value.TryChangeType(pType, out convertedValue))
        {
            throw new ArgumentException(string.Format("【{0}】的查询值类型不正确,必须为{1}类型!", General.GetDisplayName(fieldInfo), pType.FullName), "value");
        }
        ParameterExpression expParameter = Expression.Parameter(typeof(TEntity), "f");
        MemberExpression expl = Expression.Property(expParameter, fieldInfo);
        ConstantExpression expr = Expression.Constant(convertedValue, pType);
        Expression expBody = null;
        Type expType = typeof(Expression);
        var expMethod = expType.GetMethod(operatorName, new[] { expType, expType });
        if (expMethod != null)
        {
            expBody = (Expression)expMethod.Invoke(nullnew object[] { expl, expr });
        }
        else if (FilterOperators.Between == operatorName)
        {
            dynamic convertedValue2;
            if (!value2.TryChangeType(pType, out convertedValue2))
            {
                throw new ArgumentException(string.Format("【{0}】的查询值2类型不正确,必须为{1}类型!", General.GetDisplayName(fieldInfo), pType.FullName), "value");
            }
            ConstantExpression expr2 = Expression.Constant(convertedValue2, pType);
            expBody = Expression.GreaterThanOrEqual(expl, expr);
            expBody = Expression.AndAlso(expBody, Expression.LessThanOrEqual(expl, expr2));
        }
        else if (new[] { FilterOperators.Contains, FilterOperators.StartsWith, FilterOperators.EndsWith }.Contains(operatorName))
        {
            expBody = Expression.Call(expl, typeof(string).GetMethod(operatorName, new Type[] { typeof(string) }), expr);
        }
        else
        {
            throw new ArgumentException("无效的运算符!""operatorName");
        }
        Expression<Func<TEntity, bool>> lamExp = Expression.Lambda<Func<TEntity, bool>>(expBody, expParameter);
        return lamExp;
    }
  • 相关阅读:
    Mybatis中Log4j日志的使用
    Mybatis结果集ResultMap映射
    Mybatis中的基本对象的生命周期和作用域
    IAR瑞萨单片机开发加入printf调试函数
    【转】C语言mem.h中的函数介绍
    【转】c语言位域操作—_结构体内冒号:的使用
    串口数据传输当中的共用体和结构体转换
    【转】printf格式串中的%f的输出格式和内容
    【转】缓冲区设计--环形队列(C++)
    【转】环形队列理论(C语言)
  • 原文地址:https://www.cnblogs.com/shitaotao/p/7648259.html
Copyright © 2011-2022 走看看