DevExpress XPO笔记7:XPO数据过滤功能的实现(一)

XPO可以提供多种途径来实现过滤功能,比较常用的有:实现抽象类CriteriaOperator的派生类对象,和使用静态方法CriteriaOperator.Parst。

首先我们创建两个类用户和订单,它们是一对多的关系:

 窗体中创建两个列表控件,上面显示所有数据,下面显示过滤后的数据,按钮分别执行不同过滤。

CriteriaOperator的派生类有很多,比较常用的有:

  1. BinaryOperator
  2. BetweenOperator
  3. InOperator
  4. ContainsOperator
  5. NotOperator
  6. NullOperator
  7. GroupOperator
  8. AggregateOperator
  9. CriteriaOperator.Parse

1、BinaryOperator 二元表达式过滤

它的构造参数为二元表达式,主要有三种构造方法:

(字段,值)筛选出两者相等的数据

(字段,值,判断类型)判断类型有大于、等于、大于等于……

(字段,值,判断类型)两个CriteriaOperator过滤条件和判断类型与、或……

        /// <summary>
        /// 二元表达式过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBinaryOperator_Click(object sender, EventArgs e)
        {
            //Name = "c"
            CriteriaOperator bo1 = new BinaryOperator("Name", "c");
            //Age >= 22
            CriteriaOperator bo2 = new BinaryOperator("Age", 22, BinaryOperatorType.GreaterOrEqual);
            //bo1 && bo2
            CriteriaOperator bo3 = new BinaryOperator(bo1, bo2, BinaryOperatorType.BitwiseAnd);

            xpCollection1.Criteria = bo2; 
        }

 

2、BetweenOperator 两个值之间的数据过滤

查询位置两个值之间的数据:

        /// <summary>
        /// 运算符表达式过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBetweenOperator_Click(object sender, EventArgs e)
        {
            //20 <= Age <= 23
            CriteriaOperator bo = new BetweenOperator("Age", 20, 23);

            xpCollection1.Criteria = bo;
        }

3、InOperator包含于某集合的数据

        /// <summary>
        /// 包含于某集合的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInOperator_Click(object sender, EventArgs e)
        {
            //Name in { "a", "e" }
            CriteriaOperator bo = new InOperator("Name", new string[] { "a", "e" });

            xpCollection1.Criteria = bo;
        }

4、ContainsOperator包含实体类的过滤

当两个实体类之间是一对多、多对多的关系时,对多的一端的属性或字段的筛选就必须使用此类。

        /// <summary>
        /// 包含实体类时的筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnContainsOperator_Click(object sender, EventArgs e)
        {
            CriteriaOperator bo = new ContainsOperator("Orders", new BinaryOperator("Price", 32, BinaryOperatorType.GreaterOrEqual));

            xpCollection1.Criteria = bo;
        }

5、NotOperator相反关系时的筛选

与InOperator正好相反。

        /// <summary>
        /// 不包含设置条件的筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNotOperator_Click(object sender, EventArgs e)
        {
            //Name in {"a","e"}
            CriteriaOperator io = new InOperator("Name", new string[] { "a", "e" });
            //Name Not in {"a","e"}
            CriteriaOperator no = new NotOperator(io);

            xpCollection1.Criteria = no;
        }

6、NullOperator查找字段为空值的集合

        /// <summary>
        /// 查询字段为空值的集合
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNullOperator_Click(object sender, EventArgs e)
        {
            CriteriaOperator no = new NullOperator("Name");
            xpCollection1.Criteria = no;
        }

7、GroupOperator组合过滤条件

过滤条件有 GroupOperatorType.And 和 GroupOperatorType.Or

        /// <summary>
        /// 组合任意过滤条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGroupOperator_Click(object sender, EventArgs e)
        {
            CriteriaOperator bo1 = new BinaryOperator("Name", "c", BinaryOperatorType.Equal);
            CriteriaOperator bo2 = new BinaryOperator("Age", "21", BinaryOperatorType.GreaterOrEqual);
            //bo1 && bo2
            CriteriaOperator go = new GroupOperator(GroupOperatorType.And, bo1, bo2);
            xpCollection1.Criteria = go;
        }

8、AggregateOperator 一对多或多对多时对多的一端的聚合查询

        /// <summary>
        /// 一对多、多对多时对多的一端的聚合查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAggregateOperator_Click(object sender, EventArgs e)
        {
            //Customer where Customer.Orders.Sum(Price)>30
            CriteriaOperator ao = new AggregateOperand(new OperandProperty("Orders"), new OperandProperty("Price"), Aggregate.Sum, null);
            BinaryOperator bo = new BinaryOperator(ao, new OperandValue(30), BinaryOperatorType.Greater);
            xpCollection1.Criteria = bo;
        }

9、CriteriaOperator.Parse 按条件表达式过滤筛选

        /// <summary>
        /// 按条件表达式过滤筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            CriteriaOperator coParse1 = CriteriaOperator.Parse("Name = 'a' or Name = 'c'");
            CriteriaOperator coParse2 = CriteriaOperator.Parse("Name = 'a' || Name = 'c'");
            CriteriaOperator coParse3 = CriteriaOperator.Parse("Name = 'a'");
            CriteriaOperator coParse4 = CriteriaOperator.Parse("Order.Sum(Price) > 100");
            CriteriaOperator coParse5 = CriteriaOperator.Parse("Name like 'eo%' or Name like '%m'");
            CriteriaOperator coParse6 = CriteriaOperator.Parse("Name = 'a' and Age = 32");
            CriteriaOperator coParse7 = CriteriaOperator.Parse("Name in ('e', 'b', 'a')");
            CriteriaOperator coParse8 = CriteriaOperator.Parse("Age - 30 > 0");
            CriteriaOperator coParse9 = CriteriaOperator.Parse("Name = ? And Age < ?", "a", 30);

            xpCollection1.Criteria = coParse1;
        }

 

posted @ 2022-09-11 18:08  东经115  阅读(362)  评论(0编辑  收藏  举报