共用方式為


Observable.where < TSource > 方法 (IObservable < TSource > , Func < TSource, Boolean >)

根據述詞篩選可觀察序列的專案。

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

Syntax

'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

另請參閱

參考

Observable 類別

Where 多載

System.Reactive.Linq 命名空間