Queryable.Contains 方法

定義

判斷 IQueryable<T> 是否包含指定的項目。Determines whether an IQueryable<T> contains a specified element.

多載

Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>)

使用指定的 IEqualityComparer<T> 來判斷序列是否包含指定的項目。Determines whether a sequence contains a specified element by using a specified IEqualityComparer<T>.

Contains<TSource>(IQueryable<TSource>, TSource)

使用預設的相等比較子 (Comparer) 來判斷序列是否包含指定的項目。Determines whether a sequence contains a specified element by using the default equality comparer.

Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>)

使用指定的 IEqualityComparer<T> 來判斷序列是否包含指定的項目。Determines whether a sequence contains a specified element by using a specified IEqualityComparer<T>.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static bool Contains(System::Linq::IQueryable<TSource> ^ source, TSource item, System::Collections::Generic::IEqualityComparer<TSource> ^ comparer);
public static bool Contains<TSource> (this System.Linq.IQueryable<TSource> source, TSource item, System.Collections.Generic.IEqualityComparer<TSource> comparer);
public static bool Contains<TSource> (this System.Linq.IQueryable<TSource> source, TSource item, System.Collections.Generic.IEqualityComparer<TSource>? comparer);
static member Contains : System.Linq.IQueryable<'Source> * 'Source * System.Collections.Generic.IEqualityComparer<'Source> -> bool
<Extension()>
Public Function Contains(Of TSource) (source As IQueryable(Of TSource), item As TSource, comparer As IEqualityComparer(Of TSource)) As Boolean

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IQueryable<TSource>

要在其中尋找 itemIQueryable<T>An IQueryable<T> in which to locate item.

item
TSource

要在序列中尋找的物件。The object to locate in the sequence.

comparer
IEqualityComparer<TSource>

用來比較值的 IEqualityComparer<T>An IEqualityComparer<T> to compare values.

傳回

Boolean

如果輸入序列包含具有指定值的項目,則為 true,否則為 falsetrue if the input sequence contains an element that has the specified value; otherwise, false.

例外狀況

sourcenullsource is null.

備註

Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>)方法 MethodCallExpression 會產生,代表呼叫 Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) 本身做為已建立的泛型方法。The Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) method generates a MethodCallExpression that represents calling Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) itself as a constructed generic method. 然後,它會將傳遞 MethodCallExpression 給以 Execute<TResult>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) 取決於參數類型的實作為 sourceThe query behavior that occurs as a result of executing an expression tree that represents calling Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) depends on the implementation of the type of the source parameter. 預期的行為是它會 source item 使用 comparer 來比較值,以判斷是否包含。The expected behavior is that it determines if source contains item by using comparer to compare values.

適用於

Contains<TSource>(IQueryable<TSource>, TSource)

使用預設的相等比較子 (Comparer) 來判斷序列是否包含指定的項目。Determines whether a sequence contains a specified element by using the default equality comparer.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static bool Contains(System::Linq::IQueryable<TSource> ^ source, TSource item);
public static bool Contains<TSource> (this System.Linq.IQueryable<TSource> source, TSource item);
static member Contains : System.Linq.IQueryable<'Source> * 'Source -> bool
<Extension()>
Public Function Contains(Of TSource) (source As IQueryable(Of TSource), item As TSource) As Boolean

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IQueryable<TSource>

要在其中尋找 itemIQueryable<T>An IQueryable<T> in which to locate item.

item
TSource

要在序列中尋找的物件。The object to locate in the sequence.

傳回

Boolean

如果輸入序列包含具有指定值的項目,則為 true,否則為 falsetrue if the input sequence contains an element that has the specified value; otherwise, false.

例外狀況

sourcenullsource is null.

範例

下列程式碼範例將示範如何使用 Contains<TSource>(IQueryable<TSource>, TSource) 來判斷序列是否包含特定的元素。The following code example demonstrates how to use Contains<TSource>(IQueryable<TSource>, TSource) to determine whether a sequence contains a specific element.

string[] fruits = { "apple", "banana", "mango",
                    "orange", "passionfruit", "grape" };

// The string to search for in the array.
string mango = "mango";

bool hasMango = fruits.AsQueryable().Contains(mango);

Console.WriteLine(
    "The array {0} contain '{1}'.",
    hasMango ? "does" : "does not",
    mango);

// This code produces the following output:
//
// The array does contain 'mango'.
Dim fruits() As String = {"apple", "banana", "mango", _
                        "orange", "passionfruit", "grape"}

' The string to search for in the array.
Dim mango As String = "mango"

Dim hasMango As Boolean = fruits.AsQueryable().Contains(mango)

MsgBox(String.Format("The array {0} contain '{1}'.", _
        IIf(hasMango, "does", "does not"), mango))

' This code produces the following output:
'
' The array does contain 'mango'.

備註

Contains<TSource>(IQueryable<TSource>, TSource)方法 MethodCallExpression 會產生,代表呼叫 Contains<TSource>(IQueryable<TSource>, TSource) 本身做為已建立的泛型方法。The Contains<TSource>(IQueryable<TSource>, TSource) method generates a MethodCallExpression that represents calling Contains<TSource>(IQueryable<TSource>, TSource) itself as a constructed generic method. 然後,它會將傳遞 MethodCallExpression 給以 Execute<TResult>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Contains<TSource>(IQueryable<TSource>, TSource) 取決於參數類型的實作為 sourceThe query behavior that occurs as a result of executing an expression tree that represents calling Contains<TSource>(IQueryable<TSource>, TSource) depends on the implementation of the type of the source parameter. 預期的行為是它判斷是否 source 包含 itemThe expected behavior is that it determines if source contains item.

適用於