Click or drag to resize

XEnumerableSingleOrDefaultTSource Method (IEnumerableTSource, FuncTSource, Boolean, TSource)

Returns the only element of a sequence that satisfies a specified condition, or the specified default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

Namespace:  Linq.Extras
Assembly:  Linq.Extras (in Linq.Extras.dll) Version: 5.0.0+96a4f4bfed64095342c5df107c1fe1fb95603ee5
Syntax
public static TSource SingleOrDefault<TSource>(
	this IEnumerable<TSource> source,
	Func<TSource, bool> predicate,
	TSource defaultValue
)

Parameters

source
Type: System.Collections.GenericIEnumerableTSource
The sequence to return a single element from.
predicate
Type: SystemFuncTSource, Boolean
A function to test an element for a condition.
defaultValue
Type: TSource
The default value to return if the no element matches the predicate.

Type Parameters

TSource
The type of the elements of source.

Return Value

Type: TSource
The single element of the input sequence that satisfies the condition, or defaultValue if no such element is found.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. 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).
See Also