篩選

本主題的範例將示範如何根據指定的條件來篩選事件資料流。篩選是透過 where 子句來表示。也就是說,只有當 where 子句中定義的運算式評估為 true 時,篩選才會將事件傳播至輸出資料流。篩選會針對 CepStream<T> 事件運作並且產生 CepStream<T> 事件。

範例

在下列範例中,someStream 事件資料流中的事件會限制為 i 裝載欄位值大於 10 的事件。不符合這個條件的事件不會傳遞給輸出資料流。

// Assuming the following input event type:
public class MyPayload
{
    public int i;
}

var queryFilter = from c in someStream
                  where c.i > 10
                  select c;

篩選器述詞可以呼叫任何適用於執行處理序的 .NET 方法。以下範例會呼叫 Math.Abs 方法。

var queryFilter = from c in someStream
                  where Math.Abs(c.i) > 10
                  select c;

文化特性特有的屬性 (Attribute) 可以當做參數使用。以下範例指定了 CultureInfo.InvariantCulture 屬性。

var queryFilter = from c in someStream
                  where string.Compare(Convert.ToString(c.value),
                                       c.str,
                                       true,
                                       CultureInfo.InvariantCulture) > 0
                  select c;