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

后端 未结 11 1742
隐瞒了意图╮
隐瞒了意图╮ 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:45

    All(x => x.Predicate) is the opposite of Any(x => !x.Predicate) ("Are all cars red?" is the opposite of "Are there any cars that aren't red?").

    Any(x => !x.Predicate) returns false for empty collections (which appears natural for the common understanding of "any").

    Hence All(x => x.Predicate) should (and does) return true for empty collections.

提交回复
热议问题