I would like to rewrite one of my extensions LINQto take advantage of parallelism. In any case, I do not know where to start.
As an example of training, I would like to know how to rewrite operator implementation Where(), but it works on ParallelQuery.
public static ParallelQuery<TSource> Where<TSource>(
this ParallelQuery<TSource> source,
Func<TSource, bool> predicate)
{
}
One could write:
someList.AsParallel().Where(...)
The notation a Where, which is executed in sequential order, is trivial:
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
foreach (TSource item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
I meant just to wrap the predicate around Parallel.ForEach()(and by clicking the results in List / Array), but I don't think this is the way to go.
, ( SO), . , , , . , , , , , - , , , ( , , )
, LINQ, :
public static IEnumerable<TSource> WhereContains<TSource, TKey>(
this IEnumerable<TSource> source,
IEnumerable<TKey> values,
Func<TSource, TKey> keySelector)
{
HashSet<TKey> elements = new HashSet<TKey>(values);
foreach (TSource item in source)
{
if (elements.Contains(keySelector(item)))
{
yield return item;
}
}
}