许多选择在接收

问题描述:

请任何一个让我知道如何在运营商的SelectMany的接收工作。我不知道更多关于这个运营商在LINQ to。请任何一个解释这一个简单的例子的帮助下,以及在什么场合,我们将在接收使用该操作符。在此先感谢

Please any one let me know how the SelectMany operator in Rx works. I don know more about this operator in Linq to. Please any one explain this with the help of a simple example , and also in what occasion we will use this operator in Rx. Thanks in advance

的SelectMany 结合了投影和压扁成一个单一的步骤。假设你有一个数字表像 {{1,2},{3,4,5},{6,7}} 您可以使用的SelectMany 将其压扁成一个单一的列表,如: {1,2,3,4,5,6,7}

SelectMany combines projection and flattening into a single step. Suppose you have a number of lists like { {1, 2}, {3, 4, 5}, { 6, 7 } } you can use SelectMany to flatten it into a single list like: { 1, 2, 3, 4, 5, 6, 7}

的SelectMany 的接收可以拼合多个序列合并为一个可观察的(实际上有几个重载)。

SelectMany in Rx can flatten multiple sequences into one observable (there are actually several overloads).

对于一个实际的例子,假设你有一个函数 DownloadFile(文件名)它给你一个可观察其产生的值,当文件完成下载。现在,您可以这样写:

For a practical example, suppose you have a function DownloadFile(filename) which gives you an Observable which produces a value when the file completes downloading. You can now write:

string[] files = { "http://.../1", "http://.../2", "http://.../3" };

files.ToObservable()
                 .SelectMany(file => DownloadFile(file))
                 .Take(3)
                 .Subscribe(c => Console.WriteLine("Got " + c) , ()=>  Console.WriteLine("Completed!"));

DownloadFile 所有3观测值都压扁成一个,这样你就可以等待3个值到达地看到,所有下载完成。

All 3 observables of DownloadFile are flattened into one, so you can wait for 3 values to arrive to see that all downloads are completed.