0

I find it really weird that this condition returns true as a result when there are no items in the IEnumerable collection.

if(Results.All(r => r.Status == ResultStatus.Upload))

Results is an IEnumerable of objects and I'm basically checking if all its items' statuses are Upload. My workaround was first checking if the collection has items then check if all items have that status which works fine.

if(Results.Any() && Results.All(r => r.Status == ResultStatus.Upload))

Why does it not validate to false when there are zero items in the collection?

Dumisani
  • 2,988
  • 1
  • 29
  • 40
  • this is by design. It returns true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false. https://msdn.microsoft.com/en-us/library/bb548541(v=vs.110).aspx – Riv Sep 18 '17 at 11:14
  • 3
    "Why does it not validate to false when there are zero items in the collection?" Because all the items in the collection *do* meet the specified criteria. It's working as documented: "Returns: true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false." – Jon Skeet Sep 18 '17 at 11:14
  • Thank you, makes perfect sense now – Dumisani Sep 18 '17 at 11:18
  • It returns `true` because no element does not pass the test. – Olivier Jacot-Descombes Sep 18 '17 at 11:18
  • `All` assumes true before testing collection, `Any` assumes false. so if collection is empty Any returns false. – M.kazem Akhgary Sep 18 '17 at 11:21

0 Answers0