Operating nullabe in c#

这里提到

1.The predefined unary and binary operators and any user-defined operators that exist for value types may also be used by nullable types. These operators produce a null value if the operands are null; otherwise, the operator uses the contained value to calculate the result.

2.When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal). It is important not to assume that because a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, nor equal to null. Only num1 != num2 evaluates to true.

所以int?等可空类型的操作除了!=以外,和Sql基本一样,碰到null就得null或者为false.

linq join中只支持equals

这里有人问了同样的问题,估计不少人都会很自然的发出同样的疑问,没办法了,规定就是规定,而且,join的条件里必须用equals,==都不行。难道微软在join的on后面就只允许equals这一个关键字?还真是的,这里有人回答了。“LINQ directly supports only equi-joins, that is, joins based on equal conditions”

对于join里的大于小于的需求,目前只能转化到where里去。用join(inner join)关键字的话需要加恒真的on条件,或者用笛卡尔积,也就是两个from的形式。其实,T-SQL里的join也是先做笛卡尔积,再把条件转化到where里的。而对于sql中left或者right join里的大于小于的条件怎么表述呢?也是直接在笛卡尔积的linq表述下功夫。

这里是一个Linq left join的例子(right join也差不多),它实际上是新加了一层from(第三层的range variable),然后用DefaultIfEmpty判断第二层是否为空,DefaultIfEmpty有两个重载,如果第二层from得到的元素没有default,就需要用DefaultIfEmpty(new xxxx())的重载。也即是linq并不是直接支持left/right join。

oracle里的空字符串是null吗?

http://stackoverflow.com/questions/203493/why-does-oracle-9i-treat-an-empty-string-as-null

再看我们的数据库,都是varchar2类型的,默认值是‘ ’(有空格),但又是nullable的。所以再要排除空记录(包括空值和默认值),应该是

XXX is not null and xxxx <> ‘ ’(有空格) 或者 nvl(xxx,‘ ’)<> ‘ ’

后则写起来简单一些。

update:

经试验,这种做法比较简洁(必较即要判断 is null,又要判断一个空格的做法)

trim(XXX) is null

当XXX是空格或是null值(空串)的时候都成立

不为空就则改成is not null

比如

select  sop_destination_warehouse from SALES_ORDER_HEADERS where trim(sop_destination_warehouse) is null 返回的是sop_destination_warehouse为空的记录

select  sop_destination_warehouse from SALES_ORDER_HEADERS where trim(sop_destination_warehouse) is  not null 返回的是sop_destination_warehouse有值的记录。

另:安装ODP.net可以解决System.Data.OracleClient   requires   Oracle   client   software   version   8.1.7   or   greater.的错误。并且,要给予aspnet账号(iis5.1,其他版本看具体服务进程的用户)oracle client目录读写权限。