Quick question, what\'s the preferable way to programmaticaly ask \"Is there exactly one element in this sequence that satisfies X condition?\" using Linq?
i.e.
You could use this extension method, which I think should have been included in the standard extension methods for linq:
public static int CountUpTo(this IEnumerable sequence, int maxCount) {
if (sequence == null) throw new ArgumentNullException("sequence");
if (maxCount < 0) throw new ArgumentOutOfRangeException("maxCount");
var count = 0;
var enumerator = sequence.GetEnumerator();
while (count < maxCount && enumerator.MoveNext())
count += 1;
return count;
}
Used like so:
return sequence.CountUpTo(2) == 1;