Pregunta

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.

¿Fue útil?

Solución

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 =>
    {

    });

Otros consejos

Have you tried specifying the type explicitly like this.

var list = new List<string>();
Parallel.ForEach<string>(list, (s) => s.Trim());
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top