Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Observable.Where(Of TSource) Method (IObservable(Of TSource), Func(Of TSource, Boolean))

Filters the elements of an observable sequence based on a predicate.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in 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)

Type Parameters

TSource

The type source.

Parameters

source
Type: System.IObservable(Of TSource)
An observable sequence whose elements to filter.
predicate
Type: System.Func(Of TSource, Boolean)
A function to test each source element for a condition.

Return Value

Type: System.IObservable(Of TSource)
An observable sequence that contains elements from the input sequence that satisfy the condition.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IObservable(Of TSource). When you use instance method syntax to call this method, omit the first parameter. For more information, see https://msdn.microsoft.com/en-us/library/bb384936(v=vs.103).aspx or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.103).aspx.

The Where operator allows you to define a predicate function to test each item in a sequence. Each item from the source sequence which causes the predicate function to return false is filtered from the resulting sequence.

The following example uses the Where operator with a Language Integerated Query (LINQ) to filter the integer sequence so that only even integers are produced for the sequence.

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

The following output was generated by the example code.

0
2
4
6
8
10
12
14
16
Show:
© 2015 Microsoft