I looking for help on how to make use of yield keyword to return IEnumerable<T>
in parallel blocks or Task block.
This is the pseudo code:
public IEnumerable<List<T>> ReadFile( )
{
foreach (string filepath in lstOfFiles)
{
var stream = new FileStream(filepath , FileMode.Open, FileAccess.Read);
foreach (var item in ReadStream(stream))
yield return item; //where item is of type List<string>
}
}
I want to convert above code to parallel block like this:
lstOfFiles.AsParallel()
.ForAll(filepath =>
{
var stream = new FileStream(filepath , FileMode.Open, FileAccess.Read);
foreach (var item in ReadStream(Stream))
yield return item;
});
but the compiler throws an error that yield
cannot be used in parallel blocks or anonymous delegate. I tried with Task block also, yield is not allowed in task anonymous delegate
Any one suggest me simple and best way to have yield to return collection of data in parallel blocks or task.
I read that RX 2.0 or TPL are good to use in the above scenario. I have a doubt whether to make use of RX or TPL library for asynchronous return of yield of values. Can any one suggest me which is better either Rx or TPL.
If i use of Rx, is it necessary to create subscribe and convert parallel block AsObservable
.
It looks like you want to use SelectMany
. You can't use yield
in an anonymous method, but you can break this out into a new method, like so:
IEnumerable<Item> items = lstOfFiles.AsParallel()
.SelectMany(( filepath ) => ReadItems(filepath));
IEnumerable<Item> ReadItems(string filePath)
{
using(var Stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
foreach (var item in ReadStream(Stream))
yield return item;
}
}