Why does IQueryable.All() return true on an empty collection?

后端 未结 11 1716
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 04:46

So I ran into a situation today where some production code was failing precisely because a method performed exactly as documented in MSDN. Shame on me for not reading the d

11条回答
  •  失恋的感觉
    2020-12-01 05:33

    You will find this behaviour quite often in other areas of mathematics or computer science.

    The SUM operator in Math will return 0 (the neutral element of +) in cases where the ranges are invalid (the SUM from 0 up to -1). The MULTIPYL operator will return 1 (neutral element for multiplication).

    Now if you have Boolean expressions, it's quite similar: The neutral element for OR is false (a OR false = a) whereas the neutral element for AND is true.

    Now on Linq's ANY and ALL: They are similar to this:

    ANY = a OR b OR c OR d ...
    ALL = a AND b AND c AND d ...
    

    So this behavior is just what "you would expect" if you have a math/cs background.

提交回复
热议问题