从可观测序列创建列表。
Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (
语法
'Declaration
<ExtensionAttribute> _
Public Shared Function ToList(Of TSource) ( _
source As IObservable(Of TSource) _
) As IObservable(Of IList(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim returnValue As IObservable(Of IList(Of TSource))
returnValue = source.ToList()
public static IObservable<IList<TSource>> ToList<TSource>(
this IObservable<TSource> source
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<IList<TSource>^>^ ToList(
IObservable<TSource>^ source
)
static member ToList :
source:IObservable<'TSource> -> IObservable<IList<'TSource>>
JScript does not support generic types and methods.
类型参数
- TSource
源的类型。
参数
- source
类型: System.IObservable<TSource>
要获取其元素列表的源可观测序列。
返回值
类型: System.IObservable<IList<TSource>>
来自可观测序列的列表。
使用说明
在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅或。
备注
ToList 运算符采用序列中的所有项,并将其放入列表中。 然后,列表作为可观测序列返回 (IObservable<IList<TSource>>) 。 此运算符的返回值不同于 IEnumerable 上的相应运算符,以便保留异步行为。
示例
下面的示例使用 Generate 运算符生成一个简单的整数序列, (1-10) 。 然后,使用 ToList 运算符将该序列转换为列表。 在将列表中的每个项写入控制台窗口之前,使用 IList.Add 方法将 9999 写入结果列表。
using System;
using System.Reactive.Linq;
using System.Collections;
namespace Example
{
class Program
{
static void Main()
{
//*********************************************//
//*** Generate a sequence of integers 1-10 ***//
//*********************************************//
var obs = Observable.Generate(1, // Initial state value
x => x <= 10, // The termination condition. Terminate generation when false (the integer squared is not less than 1000)
x => ++x, // Iteration step function updates the state and returns the new state. In this case state is incremented by 1
x => x); // Selector function determines the next resulting value in the sequence. The state of type in is squared.
//***************************************************************************************************//
//*** Convert the integer sequence to a list. Use the IList.Add() method to add 9999 to the list ***//
//***************************************************************************************************//
var obsList = obs.ToList();
obsList.Subscribe(x =>
{
x.Add(9999);
//****************************************//
//*** Enumerate the items in the list ***//
//****************************************//
foreach (int val in x)
{
Console.WriteLine(val);
}
});
Console.WriteLine("\nPress ENTER to exit...\n");
Console.ReadLine();
}
}
}
以下输出是使用示例代码生成的。
1
2
3
4
5
6
7
8
9
10
9999
Press ENTER to exit...