Enumerable.Single Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir dizinin tek, belirli bir öğesini döndürür.
Aşırı Yüklemeler
| Name | Description |
|---|---|
| Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
Belirtilen koşulu karşılayan bir dizinin tek öğesini döndürür ve birden fazla öğe varsa bir özel durum oluşturur. |
| Single<TSource>(IEnumerable<TSource>) |
Bir dizinin tek öğesini döndürür ve dizide tam olarak bir öğe yoksa bir özel durum oluşturur. |
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
Belirtilen koşulu karşılayan bir dizinin tek öğesini döndürür ve birden fazla öğe varsa bir özel durum oluşturur.
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
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IEnumerable<TSource>
Tek bir öğe döndürmek için bir IEnumerable<T>.
Döndürülenler
Bir koşulu karşılayan giriş dizisinin tek öğesi.
Özel durumlar
source veya predicate şeklindedir null.
içindeki koşulu predicatekarşılayan öğe yok.
-veya-
predicateiçindeki koşulu birden fazla öğe karşılar.
-veya-
Kaynak sıra boş.
Örnekler
Aşağıdaki kod örneği, bir dizideki koşulu karşılayan tek öğeyi seçmek için Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) nasıl kullanılacağını gösterir.
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}")
Aşağıdaki kod örneği, dizi koşulu karşılayan tam olarak bir öğe içermediğinde bir özel durum oluşturduğunu gösterir Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) .
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.
Açıklamalar
Giriş Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) dizisi eşleşen öğe içermiyorsa yöntemi bir özel durum oluşturur. Bunun yerine eşleşen öğe bulunamadığında döndürmek null için kullanın SingleOrDefault.
Şunlara uygulanır
Single<TSource>(IEnumerable<TSource>)
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
- Kaynak:
- Single.cs
Bir dizinin tek öğesini döndürür ve dizide tam olarak bir öğe yoksa bir özel durum oluşturur.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Single(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource Single<TSource>(this System.Collections.Generic.IEnumerable<TSource> source);
static member Single : seq<'Source> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IEnumerable(Of TSource)) As TSource
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IEnumerable<TSource>
öğesinin tek öğesini döndürmek için bir IEnumerable<T>.
Döndürülenler
Giriş dizisinin tek öğesi.
Özel durumlar
source, null'e eşittir.
Örnekler
Aşağıdaki kod örneği, bir dizinin tek öğesini seçmek için Single<TSource>(IEnumerable<TSource>) nasıl kullanılacağını gösterir.
string[] fruits1 = { "orange" };
string fruit1 = fruits1.Single();
Console.WriteLine(fruit1);
/*
This code produces the following output:
orange
*/
' Create an array that contains one item.
Dim fruits1() As String = {"orange"}
' Get the single item in the array.
Dim result As String = fruits1.Single()
' Display the result.
Console.WriteLine($"First query: {result}")
Aşağıdaki kod örneği, dizi tam olarak bir öğe içermediğinde bir özel durum oluşturduğunu Single<TSource>(IEnumerable<TSource>) gösterir.
string[] fruits2 = { "orange", "apple" };
string fruit2 = null;
try
{
fruit2 = fruits2.Single();
}
catch (System.InvalidOperationException)
{
Console.WriteLine("The collection does not contain exactly one element.");
}
Console.WriteLine(fruit2);
/*
This code produces the following output:
The collection does not contain exactly one element.
*/
' Create an array that contains two items.
Dim fruits2() As String = {"orange", "apple"}
result = String.Empty
' Try to get the 'single' item in the array.
Try
result = fruits2.Single()
Catch ex As System.InvalidOperationException
result = "The collection does not contain exactly one element."
End Try
' Display the result.
Console.WriteLine($"Second query: {result}")
' This code produces the following output:
'
' First query: orange
' Second query: The collection does not contain exactly one element.
Açıklamalar
Single<TSource>(IEnumerable<TSource>) Giriş dizisi boşsa yöntemi bir özel durum oluşturur. Bunun yerine giriş dizisi boş olduğunda döndürmek null için kullanın SingleOrDefault.