ismellike
ismellike

Reputation: 629

Parallel.ForEach gives error

I've been trying to search about what I can do for my Parallel.ForEach loop:

        selection.Words is Microsoft.Office.Interop.Word.Selection;
        //range is supposed to be a word.Range
        Parallel.ForEach(selection.Words, range =>
        {

        });

This is the error I am receiving, The type arguments for method "System.Threading.Tasks.Parallel.ForEach(System.Collections.Concurrent.OrderablePartitioner, System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly."

I've been looking for a good time now, but all of them just show object.AsEnumerable() as the answer. selection.Words cannot be made into an enumberable, however.

Upvotes: 4

Views: 9167

Answers (2)

josey wales
josey wales

Reputation: 271

Have you tried specifying the type explicitly like this.

var list = new List<string>();
Parallel.ForEach<string>(list, (s) => s.Trim());

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102753

You can see that the Words type is a non-generic enumerable -- so the compiler can't infer the generic type parameter for ForEach<TSource>. You could make the collection into a typed generic collection by using OfType<Range>:

Parallel.ForEach(selection.Words.OfType<Microsoft.Office.Interop.Word.Range>(), range =>
    {

    });

Upvotes: 10

Related Questions