Enumerable.Single Method
source link: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.single?view=net-5.0
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Single(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member Single : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As TSource
Type Parameters
The type of the elements of source
.
Parameters
An IEnumerable<T> to return a single element from.
Returns
TSource
The single element of the input sequence that satisfies a condition.
Exceptions
source
or predicate
is null
.
No element satisfies the condition in predicate
.
More than one element satisfies the condition in predicate
.
The source sequence is empty.
Examples
The following code example demonstrates how to use Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to select the only element of an array that satisfies a condition.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
string fruit1 = fruits.Single(fruit => fruit.Length > 10);
Console.WriteLine(fruit1);
/*
This code produces the following output:
passionfruit
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
' Get the single item in the array whose length is greater than 10.
Dim result As String =
fruits.Single(Function(fruit) fruit.Length > 10)
' Display the result.
Console.WriteLine($"First query: {result}")
The following code example demonstrates that Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) throws an exception when the sequence does not contain exactly one element that satisfies the condition.
string fruit2 = null;
try
{
fruit2 = fruits.Single(fruit => fruit.Length > 15);
}
catch (System.InvalidOperationException)
{
Console.WriteLine(@"The collection does not contain exactly
one element whose length is greater than 15.");
}
Console.WriteLine(fruit2);
// This code produces the following output:
//
// The collection does not contain exactly
// one element whose length is greater than 15.
result = String.Empty
' Try to get the single item in the array whose length is > 15.
Try
result = fruits.Single(Function(fruit) _
fruit.Length > 15)
Catch ex As System.InvalidOperationException
result = "There is not EXACTLY ONE element whose length is > 15."
End Try
' Display the result.
Console.WriteLine($"Second query: {result}")
' This code produces the following output:
'
' First query: passionfruit
' Second query: There is not EXACTLY ONE element whose length is > 15.
Remarks
The Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if the input sequence contains no matching element. To instead return null
when no matching element is found, use SingleOrDefault.
Applies to
.NET 5.0 and other versionsRecommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK