Welcome 微信登录

首页 / 网页编程 / ASP.NET / 数据库组件 Hxj.Data (十一) (where条件)

数据库组件 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

也就是 && 表示左右条件同时成立

|| 表示左右条件有一个成立即可

组件重载了操作符:

C#SQL
>>
>=>=
<=<=
<<
===
!=<>

写法和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中生成条件的方法。