Observable.Where<TSource> 方法 (IObservable<TSource>、Func<TSource、布尔值>)

根据谓词筛选可观测序列的元素。

Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (

语法

'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
    类型源。

参数

  • predicate
    类型: System.Func<TSource、 布尔值>
    用于测试条件的每个源元素的函数。

返回值

类型: System.IObservable<TSource>
一个可观测序列,其中包含输入序列中满足条件的元素。

使用说明

在 Visual Basic 和 C# 中,可以在 IObservable<TSource> 类型的任何对象上调用此方法作为实例方法。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅

备注

Where 运算符允许定义谓词函数来测试序列中的每个项。 源序列中导致谓词函数返回 false 的每个项都从生成的序列中筛选。

示例

以下示例使用 Where 运算符和语言整数查询 (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

另请参阅

参考

可观测类

其中重载

System.Reactive.Linq 命名空间