Observable.Where<TSource> メソッド (IObservable<TSource>、Func<TSource、Boolean>)

述語に基づいて監視可能なシーケンスの要素をフィルター処理します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'Declaration
<ExtensionAttribute> _
Public Shared Function Where(Of TSource) ( _
    source As IObservable(Of TSource), _
    predicate As Func(Of TSource, Boolean) _
) As IObservable(Of TSource)
'Usage
Dim source As IObservable(Of TSource)
Dim predicate As Func(Of TSource, Boolean)
Dim returnValue As IObservable(Of TSource)

returnValue = source.Where(predicate)
public static IObservable<TSource> Where<TSource>(
    this IObservable<TSource> source,
    Func<TSource, bool> predicate
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<TSource>^ Where(
    IObservable<TSource>^ source, 
    Func<TSource, bool>^ predicate
)
static member Where : 
        source:IObservable<'TSource> * 
        predicate:Func<'TSource, bool> -> IObservable<'TSource> 
JScript does not support generic types and methods.

型パラメーター

  • TSource
    型のソース。

パラメーター

  • source
    種類: System.IObservable<TSource>
    フィルター処理する要素を持つ監視可能なシーケンス。

戻り値

種類: System.IObservable<TSource>
条件を満たす入力シーケンスの要素を含む監視可能なシーケンス。

使用上の注意

Visual Basic および C# では、 IObservable<TSource> 型の任意のオブジェクトでインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。

解説

Where 演算子を使用すると、シーケンス内の各項目をテストする述語関数を定義できます。 述語関数が false を返す原因となるソース シーケンスの各項目は、結果のシーケンスからフィルター処理されます。

次の例では、Where 演算子と Language Integerated Query (LINQ) を使用して整数シーケンスをフィルター処理し、シーケンスに対して偶数の整数のみが生成されるようにします。

using System;
using System.Reactive.Linq;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************//
      //*** The mainSequence produces a new long integer from the Interval operator every sec.    ***//
      //*********************************************************************************************//

      var mainSequence = Observable.Interval(TimeSpan.FromSeconds(1));


      //*********************************************************************************************//
      //*** This LINQ statement uses the Where operator to filter the integers in the sequence so ***//
      //*** that only the even integers are returned.                                             ***//
      //***                                                                                       ***//
      //*** you could also call the method explicitly. For example...                             ***//
      //***                                                                                       ***//
      //*** var seqWhereEven = mainSequence.Where(x => x % 2 == 0);                               ***//
      //***                                                                                       ***//
      //*********************************************************************************************//

      var seqWhereEven = from i in mainSequence
                         where i % 2 == 0 
                         select i;


      //*********************************************************************************************//
      //*** Create a subscription and write each of the even integers to the console window.      ***//
      //*********************************************************************************************//

      seqWhereEven.Subscribe(x => Console.WriteLine(x)); 
      Console.ReadLine();
    }
  }
}

コード例によって次の出力が生成されました。

0
2
4
6
8
10
12
14
16

参照

リファレンス

Observable クラス

Where Overload

System.Reactive.Linq 名前空間