FallbackIfEmptyExtensionFallbackIfEmptyT(IEnumerableT, T) Method
Returns the elements of the specified sequence or the specified
value in a singleton collection if the sequence is empty.
Namespace: MoreLinq.ExtensionsAssembly: MoreLinq (in MoreLinq.dll) Version: 4.4.0+6d97c3b1d482f98300f4446df14742b0e3fafbec
public static IEnumerable<T> FallbackIfEmpty<T>(
this IEnumerable<T> source,
T fallback
)
<ExtensionAttribute>
Public Shared Function FallbackIfEmpty(Of T) (
source As IEnumerable(Of T),
fallback As T
) As IEnumerable(Of T)
public:
[ExtensionAttribute]
generic<typename T>
static IEnumerable<T>^ FallbackIfEmpty(
IEnumerable<T>^ source,
T fallback
)
[<ExtensionAttribute>]
static member FallbackIfEmpty :
source : IEnumerable<'T> *
fallback : 'T -> IEnumerable<'T>
- source IEnumerableT
- The source sequence.
- fallback T
- The value to return in a singleton
collection if source is empty.
- T
- The type of the elements in the sequences.
IEnumerableT
An
IEnumerableT that contains
fallback
if
source is empty; otherwise,
source.
In Visual Basic and C#, you can call this method as an instance method on any object of type
IEnumerableT. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
var numbers = new[] { 123, 456, 789 };
var result = numbers.Where(x => x == 100).FallbackIfEmpty(-1).Single();
The
result variable will contain
-1.