zoukankan      html  css  js  c++  java
  • 巧用表达式树动态构建查询条件

    项目中有个高级查询的功能,根据不同查询条件的各种组合来查询mongo,这种情况,可使用Expression来动态拼接查询条件来简化操作

    BLL中相关代码如下

    public DataResult GetRateStaticList(object param, bool paginating = false)
            {
                object result = null;
                long count = 0;
                var dicParam = FromParam(param);
    
                ParameterExpression p_rate = Expression.Parameter(typeof(Entity.RateStaticMessage), "rate");
    
                MemberExpression m_chain = Expression.PropertyOrField(p_rate, "Chain");
                ConstantExpression c_chain = Expression.Constant(dicParam["Chain"]);
                var q_chain = Expression.Equal(m_chain, c_chain);
                if (dicParam.ContainsKey("Hotel"))
                {
                    MemberExpression m_hotel = Expression.PropertyOrField(p_rate, "Hotel");
                    ConstantExpression c_hotel = Expression.Constant(dicParam["Hotel"]);
                    var q_hotel = Expression.Equal(m_hotel, c_hotel);
                    q_chain = Expression.AndAlso(q_chain, q_hotel);
                }
                if (dicParam.ContainsKey("Channel"))
                {
                    MemberExpression m_hotel = Expression.PropertyOrField(p_rate, "Channel");
                    ConstantExpression c_hotel = Expression.Constant(dicParam["Channel"]);
                    var q_hotel = Expression.Equal(m_hotel, c_hotel);
                    q_chain = Expression.AndAlso(q_chain, q_hotel);
                }
                if (dicParam.ContainsKey("RatePlan"))
                {
                    MemberExpression m_hotel = Expression.PropertyOrField(p_rate, "RatePlan");
                    ConstantExpression c_hotel = Expression.Constant(dicParam["RatePlan"]);
                    var q_hotel = Expression.Equal(m_hotel, c_hotel);
                    q_chain = Expression.AndAlso(q_chain, q_hotel);
                }
    
                MemberExpression m_start = Expression.PropertyOrField(p_rate, "Start");
                ConstantExpression c_start = Expression.Constant(dicParam["Start"].ToString());
                var q_start = Expression.LessThanOrEqual(Expression.Call(m_start, typeof(string).GetMethod("CompareTo", new Type[] { typeof(string) }), c_start), Expression.Constant(0));
                q_chain = Expression.AndAlso(q_chain, q_start);
    
                MemberExpression m_end = Expression.PropertyOrField(p_rate, "End");
                ConstantExpression c_end = Expression.Constant(dicParam["End"].ToString());
                var q_end = Expression.GreaterThanOrEqual(Expression.Call(m_end, typeof(string).GetMethod("CompareTo", new Type[] { typeof(string) }), c_end), Expression.Constant(0));
                q_chain = Expression.AndAlso(q_chain, q_end);
    
                var exp = Expression.Lambda<Func<Entity.RateStaticMessage, Boolean>>(q_chain, p_rate);
                if (paginating)
                    result = _dal.List(exp, true, Convert.ToInt32(dicParam["Offset"]), Convert.ToInt32(dicParam["Limit"]));
                else
                    result = _dal.List(exp);
                count = _dal.Count(exp);
    
                return FromObject(result, count);
            }
    

     记录如上,以后遇到相同情况可参考。

  • 相关阅读:
    Tomcat集群Cluster实现原理剖析[转] 文件同步
    看到一个比较好的jbpm教程,感谢一下
    vi显示行号
    安装apache2参数详解
    Windows平台下查看占用端口的程序
    struts2中使用token避免重复提交
    在window下安装开源的中文界面的项目管理软件Redmine
    Java中数据存储
    求素数算法网摘
    模式工程化实现及扩展读书笔记——设计原则
  • 原文地址:https://www.cnblogs.com/seastar1989/p/6694395.html
Copyright © 2011-2022 走看看