13

How can I find if my list contains one and only one item without Count or Single?

Possible dupe of Efficient Linq Enumerable's 'Count() == 1' test

g t
  • 7,287
  • 7
  • 50
  • 85
Ray
  • 45,695
  • 27
  • 126
  • 169

3 Answers3

26

How about this:

int limitedCount = myEnumerable.Take(2).Count();

That will give you:

  • 0 if it was empty
  • 1 if it had exactly 1 element
  • 2 if it had 2 or more elements

... but it gives you those answers whilst only iterating over the sequence once. You can then switch on the results.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I had that there so I can tell the difference between an empty sequence and one that has more than one. In my code I first check for !Any() and handle the empty case, and then I need to handle the more than one case (with the code you have above). – Ray Sep 16 '11 at 16:43
  • Or to put it another way, this answer tells me if I have 0 or more than 1. Not if I just have more than one. – Ray Sep 16 '11 at 16:45
  • 1
    @Ray: It's not clear what you mean... are you saying that your question is basically asking the wrong thing? You only *asked* for whether a sequence contains more than one item - which is all my answer tells you. If you want *three* different possible results, the code would be slightly different - will edit with an answer for that. – Jon Skeet Sep 16 '11 at 16:47
  • You're right, I got confused. (5pm on a Friday will do that). What I meant to ask was, how can I tell if I just have one item in the list without using single. Will leave question as it stands otherwise everyone has to change their answer. – Ray Sep 16 '11 at 16:51
  • 1
    Now that the OP has clarified their requirements it looks like a dupe of this: http://stackoverflow.com/questions/6059643/efficient-linq-enumerables-count-1-test/6059711#6059711 – LukeH Sep 16 '11 at 16:52
2
myEnumerable.Take(2).Count() < 2
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
-1

rather than use the exception, loop through the enumerable and as soon as you hit more than 1, break out of it.

You don't have to count them all...just 2 of them :)

Also, using exceptions for flow-control is a bad idea because its really expensive from a performance standpoint.


As @Jon Skeet's answer shows, there are better ways to do this since the IEnumerable interface is so well designed. If it didn't have the .Skip and .Any() methods, however (which would apply to other languages or places where you are only simply iterating), you only need to count to 2.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
  • 1
    That's exactly what `Skip(1).Any()` will do - but using the built-in methods requires a lot less code :) – Jon Skeet Sep 16 '11 at 16:40
  • @Jon Skeet - I've been in Java land too long and wasn't remembering the IEnumerable API. Just went with basic logic, which would also apply to other languages (like Java) that may not have as nice an interface as IEnumerable. You're very correct though...less code is better. – cdeszaq Sep 16 '11 at 16:42
  • 2
    It's not really IEnumerable - it's the Enumerable extension methods. You can write those in Java too, of course, although not as extension methods. – Jon Skeet Sep 16 '11 at 16:49