private static FixedSingleResultOpt <TSource> WhereSingleImmediate <TSource>(this IEnumerable <TSource> source, Func <TSource, bool> predicate) { const bool usingPredicate = true; if (source is IOpt <TSource> sourceOpt) { return(SingleResultOpt.GetFixedFromOpt(sourceOpt.ToFixed().WhereRaw(predicate), usingPredicate)); } else { bool hasValue = false; TSource value = default; foreach (var element in source) { if (predicate(element)) { if (hasValue) { return(SingleResultOpt.GetFixedMoreThanOne <TSource>(usingPredicate)); } else { value = element; hasValue = true; } } } return(SingleResultOpt.GetFixedFromValue(hasValue, value, usingPredicate)); } }
private static FixedSingleResultOpt <TSource> WhereSingleImmediate <TSource>(this IEnumerable <TSource> source) { const bool usingPredicate = false; if (source is IOpt <TSource> sourceOpt) { return(SingleResultOpt.GetFixedFromOpt(sourceOpt.ToFixed(), usingPredicate)); } else if (source is IList <TSource> sourceList) { if (sourceList.Count > 1) { return(SingleResultOpt.GetFixedMoreThanOne <TSource>(usingPredicate)); } else if (sourceList.Count == 0) { return(SingleResultOpt.GetFixedZero <TSource>(usingPredicate)); } else { return(SingleResultOpt.GetFixedOne(usingPredicate, sourceList[0])); } } else { using (var sourceEnumerator = source.GetEnumerator()) { return(EnumeratorSingleToFixedOpt(sourceEnumerator, usingPredicate)); } } }