共用方式為


Queryable.TakeWhile 方法

定義

只要某一特定條件為真,便回傳序列中的元素,然後跳過剩餘的元素。

多載

名稱 Description
TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

只要指定的條件為 true,就會從序列傳回專案。

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)

只要指定的條件為 true,就會從序列傳回專案。 元素的索引用於述詞函式的邏輯中。

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

只要指定的條件為 true,就會從序列傳回專案。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ TakeWhile(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> TakeWhile<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 System.Linq.IQueryable<TSource> TakeWhile<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> System.Linq.IQueryable<'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 TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As IQueryable(Of TSource)

類型參數

TSource

元素 source的類型。

參數

source
IQueryable<TSource>

要從中回傳元素的序列。

predicate
Expression<Func<TSource,Boolean>>

一個用來測試每個元素條件的函數。

傳回

IQueryable<TSource>

一個 IQueryable<T> 包含在該測試 predicate 不再通過的元素之前的輸入序列元素的 。

屬性

例外狀況

sourcepredicatenull

範例

以下程式碼範例示範如何從 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 序列起始時回傳元素,只要條件為真。

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

// Take strings from the array until a string
// that is equal to "orange" is found.
IEnumerable<string> query =
    fruits.AsQueryable()
    .TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);

foreach (string fruit in query)
    Console.WriteLine(fruit);

/*
    This code produces the following output:

    apple
    banana
    mango
*/
Dim fruits() As String = {"apple", "banana", "mango", "orange", _
                      "passionfruit", "grape"}

' Take strings from the array until a string
' that is equal to "orange" is found.
Dim query = fruits.AsQueryable() _
    .TakeWhile(Function(fruit) String.Compare("orange", fruit, True) <> 0)

Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next

' Display the output.
MsgBox(output.ToString())

'This code produces the following output:

'apple
'banana
'mango

備註

此方法至少有一個型別 Expression<TDelegate> 參數,其型別參數的參數是其中一個 Func<T,TResult> 型別。 對於這些參數,你可以輸入 lambda 運算式,然後它會被編譯成 Expression<TDelegate>

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 方法產生的 是 MethodCallExpression ,代表 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpressionCreateQuery(Expression)IQueryProvider由參數Provider性質source所表示的方法。

執行代表呼叫 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是對 中的source每個元素都適用predicate,直到找到一個元素使predicatefalse得 返回 。 它會回傳到那個點之前的所有元素。

適用於

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

只要指定的條件為 true,就會從序列傳回專案。 元素的索引用於述詞函式的邏輯中。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ TakeWhile(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, int, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> TakeWhile<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,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 System.Linq.IQueryable<TSource> TakeWhile<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
static member TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, int, bool>> -> System.Linq.IQueryable<'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 TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, int, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Integer, Boolean))) As IQueryable(Of TSource)

類型參數

TSource

元素 source的類型。

參數

source
IQueryable<TSource>

要從中回傳元素的序列。

predicate
Expression<Func<TSource,Int32,Boolean>>

一個用來測試每個元素條件的函數;函數的第二個參數代表源序列中元素的索引。

傳回

IQueryable<TSource>

一個 IQueryable<T> 包含在該測試 predicate 不再通過的元素之前的輸入序列元素的 。

屬性

例外狀況

sourcepredicatenull

範例

以下程式碼範例示範如何從 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 序列開始時回傳元素,只要條件為真,該元素索引為真即可。

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

// Take strings from the array until a string whose length
// is less than its index in the array is found.
IEnumerable<string> query =
    fruits.AsQueryable()
    .TakeWhile((fruit, index) => fruit.Length >= index);

foreach (string fruit in query)
    Console.WriteLine(fruit);

/*
    This code produces the following output:

    apple
    passionfruit
    banana
    mango
    orange
    blueberry
*/
Dim fruits() As String = _
    {"apple", "passionfruit", "banana", "mango", _
     "orange", "blueberry", "grape", "strawberry"}

' Take strings from the array until a string whose length
' is less than its index in the array is found.
Dim query = fruits.AsQueryable() _
    .TakeWhile(Function(fruit, index) fruit.Length >= index)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
MsgBox(output.ToString())

' This code produces the following output:

' apple
' passionfruit
' banana
' mango
' orange
' blueberry

備註

此方法至少有一個型別 Expression<TDelegate> 參數,其型別參數的參數是其中一個 Func<T,TResult> 型別。 對於這些參數,你可以輸入 lambda 運算式,然後它會被編譯成 Expression<TDelegate>

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 方法產生的 是 MethodCallExpression ,代表 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpressionCreateQuery(Expression)IQueryProvider由參數Provider性質source所表示的方法。

執行代表呼叫 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是對 中的source每個元素都適用predicate,直到找到一個元素使predicatefalse得 返回 。 它會回傳到那個點之前的所有元素。 每個來源元素的索引作為 的 predicate第二個參數提供。

適用於