Queryable.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>(IQueryable<TSource>, Expression<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>(IQueryable<TSource>) |
Bir dizinin tek öğesini döndürür ve dizide tam olarak bir öğe yoksa bir özel durum oluşturur. |
Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.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::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource Single<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource Single<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Single : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Single : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IQueryable<TSource>
Tek bir öğe döndürmek için bir IQueryable<T>.
- predicate
- Expression<Func<TSource,Boolean>>
Bir koşul için bir öğeyi test etmek için bir işlev.
Döndürülenler
predicatekoşulunu karşılayan giriş dizisinin tek öğesi.
- Öznitelikler
Ö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>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) nasıl kullanılacağını gösterir.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
// Get the only string in the array whose length is greater than 10.
string fruit1 = fruits.AsQueryable().Single(fruit => fruit.Length > 10);
Console.WriteLine("First Query: " + fruit1);
try
{
// Try to get the only string in the array
// whose length is greater than 15.
string fruit2 = fruits.AsQueryable().Single(fruit => fruit.Length > 15);
Console.WriteLine("Second Query: " + fruit2);
}
catch (System.InvalidOperationException)
{
Console.Write("Second Query: The collection does not contain ");
Console.WriteLine("exactly one element whose length is greater than 15.");
}
/*
This code produces the following output:
First Query: passionfruit
Second Query: The collection does not contain exactly one
element whose length is greater than 15.
*/
Dim fruits() As String = _
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
' Get the only string in the array whose length is greater than 10.
Dim result As String = _
fruits.AsQueryable().Single(Function(fruit) fruit.Length > 10)
' Display the result.
MsgBox("First Query: " & result)
Try
' Try to get the only string in the array
' whose length is greater than 15.
Dim fruit2 As String = fruits.AsQueryable().Single(Function(fruit) fruit.Length > 15)
MsgBox("Second Query: " + fruit2)
Catch
Dim text As String = "Second Query: The collection does not contain "
text = text & "exactly one element whose length is greater than 15."
MsgBox(text)
End Try
' This code produces the following output:
' First Query: passionfruit
' Second Query: The collection does not contain exactly one
' element whose length is greater than 15.
Açıklamalar
Bu yöntem, tür bağımsız değişkeni türlerden Expression<TDelegate> biri olan en az bir tür parametresine Func<T,TResult> sahiptir. Bu parametreler için bir lambda ifadesi geçirebilirsiniz ve bu ifade bir Expression<TDelegate>olarak derlenir.
yöntemi, Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) bir oluşturur. Ardından parametresinin özelliği tarafından MethodCallExpression temsil edilen yöntemine Execute<TResult>(Expression)IQueryProvider iletirProvider.source
Çağrıyı Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, sourcetarafından belirtilen koşulu karşılayan tek öğeyi predicate döndürmesidir.
Şunlara uygulanır
Single<TSource>(IQueryable<TSource>)
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.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::Linq::IQueryable<TSource> ^ source);
public static TSource Single<TSource>(this System.Linq.IQueryable<TSource> source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource Single<TSource>(this System.Linq.IQueryable<TSource> source);
static member Single : System.Linq.IQueryable<'Source> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Single : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function Single(Of TSource) (source As IQueryable(Of TSource)) As TSource
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IQueryable<TSource>
öğesinin tek öğesini döndürmek için bir IQueryable<T>.
Döndürülenler
Giriş dizisinin tek öğesi.
- Öznitelikler
Özel durumlar
source, null'e eşittir.
Örnekler
Aşağıdaki kod örneği, bir dizinin tek öğesini seçmek için Single<TSource>(IQueryable<TSource>) nasıl kullanılacağını gösterir.
// Create two arrays.
string[] fruits1 = { "orange" };
string[] fruits2 = { "orange", "apple" };
// Get the only item in the first array.
string fruit1 = fruits1.AsQueryable().Single();
Console.WriteLine("First query: " + fruit1);
try
{
// Try to get the only item in the second array.
string fruit2 = fruits2.AsQueryable().Single();
Console.WriteLine("Second query: " + fruit2);
}
catch (System.InvalidOperationException)
{
Console.WriteLine(
"Second query: The collection does not contain exactly one element."
);
}
/*
This code produces the following output:
First query: orange
Second query: The collection does not contain exactly one element
*/
' Create two arrays.
Dim fruits1() As String = {"orange"}
Dim fruits2() As String = {"orange", "apple"}
' Get the only item in the first array.
Dim result As String = fruits1.AsQueryable().Single()
' Display the result.
MsgBox("First query: " & result)
Try
' Try to get the only item in the second array.
Dim fruit2 As String = fruits2.AsQueryable().Single()
MsgBox("Second query: " + fruit2)
Catch
MsgBox("Second query: The collection does not contain exactly one element.")
End Try
' This code produces the following output:
' First query: orange
' Second query: The collection does not contain exactly one element.
Açıklamalar
yöntemi, Single<TSource>(IQueryable<TSource>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran Single<TSource>(IQueryable<TSource>) bir oluşturur. Ardından parametresinin özelliği tarafından MethodCallExpression temsil edilen yöntemine Execute<TResult>(Expression)IQueryProvider iletirProvider.source
Çağrıyı Single<TSource>(IQueryable<TSource>) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, sourceiçindeki tek öğeyi döndürmesidir.