数据库组件 Hxj.Data (十一) (where条件)2011-07-21 博客园 steven hu在前几节的例子中都生成where之后的条件,例如:Products._.CategoryID == 2代码中这些就是生成条件,对应sql就是 categoryid=2归根到底这句代码返回回来的是一个WhereClip.WhereClip where = WhereClip.All;这个是一个空值,也就是无条件。不会生成where条件。条件的组合两个条件同时成立:Products._.UnitPrice > 1 && Products._.CategoryID == 2等效sql就是 unitprice>1 and categoryid=2两个条件或Products._.UnitPrice > 1 || Products._.CategoryID == 2等效sql就是 unitprice>1 or categoryid=2也就是 && 表示左右条件同时成立|| 表示左右条件有一个成立即可组件重载了操作符:
写法和sql类似,也方便记忆与书写。当然也可等效写为:
WhereClip where = WhereClip.All;
where = where.And(Products._.UnitPrice > 1);
where = where.Or(Products._.CategoryID == 2);where.And 等效于 &&where.Or 等效于 ||优先级可用()括起来,如下
(Products._.ProductName.Contain("apple") && Products._.UnitPrice > 1) || Products._.CategoryID == 2等效sql就是 (productname like ‘%apple%" and unitprice>1) or categoryid=2也等效于
WhereClip where = WhereClip.All;
where = where.And(Products._.ProductName.Contain("apple"));
where = where.And(Products._.UnitPrice > 1);
where = where.Or(Products._.CategoryID == 2);下一节将讲述Field中生成条件的方法。