Queryable.Contains Queryable.Contains Queryable.Contains Queryable.Contains Method

定義

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

多載

Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) 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) Contains<TSource>(IQueryable<TSource>, TSource) Contains<TSource>(IQueryable<TSource>, TSource) 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>) Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) 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);
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 TSource TSource TSource

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

comparer
IEqualityComparer<TSource>

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

傳回

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

例外狀況

備註

方法會產生, 其代表以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)至由Provider source參數IQueryProvider的屬性所表示之的方法。It 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>)之運算式樹狀架構而產生的查詢行為, 取決於source參數類型的實作為。The 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 comparer來判斷item是否包含, 以比較值。The expected behavior is that it determines if source contains item by using comparer to compare values.

Contains<TSource>(IQueryable<TSource>, TSource) Contains<TSource>(IQueryable<TSource>, TSource) Contains<TSource>(IQueryable<TSource>, TSource) 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 TSource TSource TSource

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

傳回

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

例外狀況

範例

下列程式碼範例示範如何使用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)至由Provider source參數IQueryProvider的屬性所表示之的方法。It 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)之運算式樹狀架構而產生的查詢行為, 取決於source參數類型的實作為。The 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.

適用於