Queryable.TakeWhile メソッド

定義

指定された条件を満たされる限り、シーケンスから要素を返した後、残りの要素をスキップします。

オーバーロード

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

指定された条件が満たされる限り、シーケンスから要素を返します。

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

指定された条件が満たされる限り、シーケンスから要素を返します。 要素のインデックスは、述語関数のロジックで使用されます。

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

指定された条件が満たされる限り、シーケンスから要素を返します。

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

predicate で指定されたテストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IQueryable<T>

例外

source または predicatenull です。

次のコード例は、条件が true である限り、シーケンスの先頭から要素を返すために使用 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> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターの場合は、ラムダ式を渡すと、 Expression<TDelegate>.

このメソッドは TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)MethodCallExpression 構築されたジェネリック メソッドとしての呼び出し TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 自体を表す a を生成します。 次に、パラメーターのMethodCallExpressionCreateQuery(Expression)プロパティsourceで表されるメソッドIQueryProviderProvider渡します。

呼び出し TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) を表す式ツリーを実行した結果として発生するクエリ動作は、パラメーターの型の source 実装によって異なります。 予期される動作は、返falseされる要素sourceが見つかるまで各要素に適用predicateされますpredicate。 その時点までのすべての要素が返されます。

適用対象

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

指定された条件が満たされる限り、シーケンスから要素を返します。 要素のインデックスは、述語関数のロジックで使用されます。

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

各要素が条件を満たしているかどうかをテストする関数。この関数の 2 つ目のパラメーターは、ソース シーケンスの要素のインデックスを表します。

戻り値

IQueryable<TSource>

predicate で指定されたテストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IQueryable<T>

例外

source または predicatenull です。

次のコード例では、要素のインデックスを使用 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) する条件が true である限り、シーケンスの先頭から要素を返す方法を示します。

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> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターの場合は、ラムダ式を渡すと、 Expression<TDelegate>.

このメソッドは TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)MethodCallExpression 構築されたジェネリック メソッドとしての呼び出し TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 自体を表す a を生成します。 次に、パラメーターのMethodCallExpressionCreateQuery(Expression)プロパティsourceで表されるメソッドIQueryProviderProvider渡します。

呼び出し TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) を表す式ツリーを実行した結果として発生するクエリ動作は、パラメーターの型の source 実装によって異なります。 予期される動作は、返falseされる要素sourceが見つかるまで各要素に適用predicateされますpredicate。 その時点までのすべての要素が返されます。 各ソース要素のインデックスは、次の 2 番目の引数 predicateとして指定されます。

適用対象