M
M
Mental_Force2014-02-28 17:25:05
.NET
Mental_Force, 2014-02-28 17:25:05

How to implement changing the elements of the input sequence using the PLINQ ForAll(Action) extension method?

I can't figure out how to modify the elements of the input sequence using the ForAll(lamda) extension method.
Let's say we have an array:

string[] lines = { "max9", "cat", " joe", "line", "jgeg9nj", "ergeg",
                               "egerg", "egreg4g", "29fweggf", "wfw99efyopkb" };

I want to add a label to the end of every line that contains "9":
lines.AsParallel().Where(l=> l.Contains("9")==true)
                .ForAll(l => l+="MARK");

// Убеждаемся что последовательность осталась без изменений, а жаль
foreach (var line in lines)
                Console.WriteLine(line);

            Console.ReadLine();

I suspect that I am only modifying a copy of the element, not the element itself. How to get a reference to a string instance inside a lambda?
I emphasize that I need to do this using PLINQ, or rather the ForAll method, which is performed on each of the elements of the sequence. Do not offer obvious ways to do it in a loop using regular linq.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aush, 2014-02-28
@Mental_Force

lines = lines.AsParallel().Select(l => l.Contains("9") ? l + "MARK" : l).ToArray();

Select will process the elements in parallel.

M
Mental_Force, 2014-02-28
@Mental_Force

Thank you for your attention, but I did not understand how this would help me. By the way, I determined that if you use any other objects as elements of the collection, then there are no problems. For example, this code works correctly:

class cl
 {
 public string name;
 public string city;

 public cl(string n, string c)
 {
 name = n;
 city = c;
 }
 }

 List<storage> = new List();
 storage.Add(new cl("wfwf","wefwe" ));
 storage.Add(new cl("wfwf", "wefwe"));
 storage.Add(new cl("wfwf", "wefwe"));

 storage.AsParallel().Select(i => i).ForAll(i =>i.name="qq");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question