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。