3

I am reading the answer to this question: (Is there a better way of calling LINQ Any + NOT All?). Why does this handle not all conditions? Sorry for creating a new question, but I don't have enough reputation to add a comment on the original question.

 var anyButNotAll = mySequence
.Select(item => item.SomeStatus == SomeConst)
.Distinct()
.Take(2)
.Count() == 2;
Community
  • 1
  • 1
bugfixer
  • 55
  • 7
  • Asking separate question is proper thing anyway in such case. Please make sure to update your post with information that you *do* understand (like what are results of each operation individually, maybe even split into separate statements with good names for results). – Alexei Levenkov May 04 '15 at 14:31

1 Answers1

2

If the condition is always false (or always true) then when projecting the sequence using the condition and calling Distinct there will be 1 result, not two, so Count() == 2 will return false, not true.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • But item could have other properties that are different, right ? So for example SomeStatus is same for all items but therer is SomeOtherProperty that is different. Wouldn't Distinct return more than one in that case ? – bugfixer May 04 '15 at 14:26
  • 3
    @bugfixer The collection at that point is a collection of booleans, as the sequence has been projected into a sequence of bools where each represents whether that item meets the condition. The sequence no longer represents the actual item, so whether the item is distinct in the original collection of items is irrelevant. – Servy May 04 '15 at 14:28