Queryable.Single Yöntem

Tanım

Bir dizinin tek, belirli bir öğesini döndürür.

Aşırı Yüklemeler

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>>)

Bir dizide belirtilen koşulu karşılayan tek öğeyi döndürür ve birden fazla öğe varsa özel durum oluşturur.

Single<TSource>(IQueryable<TSource>)

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);
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 IQueryable<T> tek öğesini döndürmek için.

Döndürülenler

TSource

Giriş dizisinin tek öğesi.

Özel durumlar

source, null değeridir.

source birden fazla öğeye sahiptir.

-veya-

Kaynak dizisi boş.

Örnekler

Aşağıdaki kod örneği, bir dizinin tek öğesini seçmek için nasıl kullanılacağını Single<TSource>(IQueryable<TSource>) 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ğırmayı Single<TSource>(IQueryable<TSource>) temsil eden bir oluşturur. Daha sonra parametresinin özelliği tarafından Provider temsil edilen yöntemine IQueryProvidersource iletirMethodCallExpression.Execute<TResult>(Expression)

Ç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ış, içindeki tek öğeyi döndürmesidir source.

Şunlara uygulanır

Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs

Bir dizide belirtilen koşulu karşılayan tek öğeyi döndürür ve birden fazla öğe varsa ö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);
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>

Öğesinden IQueryable<T> tek bir öğe döndürmek için.

predicate
Expression<Func<TSource,Boolean>>

Bir koşul için bir öğeyi test etmek için bir işlev.

Döndürülenler

TSource

giriş dizisinin içindeki predicatekoşulu karşılayan tek öğesi.

Özel durumlar

source veya predicate şeklindedir null.

hiçbir öğe içindeki predicatekoşulu karşılar.

-veya-

birden fazla öğe içindeki predicatekoşulu karşılar.

-veya-

Kaynak dizisi boş.

Örnekler

Aşağıdaki kod örneği, bir dizideki koşulu karşılayan tek öğeyi seçmek için nasıl kullanılacağını Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 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 türünde en az bir parametreye 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ğırmayı Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) temsil eden bir oluşturur. Daha sonra parametresinin özelliği tarafından Provider temsil edilen yöntemine IQueryProvidersource iletirMethodCallExpression.Execute<TResult>(Expression)

Ç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ış, tarafından predicatebelirtilen koşulu karşılayan tek öğeyi source döndürmesidir.

Şunlara uygulanır