I need to change items in a list using nested for/foreach loops. Problem is that I could not get it working using LINQ with or without dot notation. The traditional way worked and goes like this:
foreach (MapObjectLayer mapObjectLayer in map.Objects)
{
foreach (MapObject mapObject in mapObjectLayer.MapObjects)
{
for (int i = 0; i < mapObject.Points.Count; i++)
{
mapObject.Points[i] = new Vector2(
mapObject.Points[i].X * map.Scale,
mapObject.Points[i].Y * map.Scale);
}
}
}
using LINQ, this failed:
var test = (from mol in map.Objects
from mo in mol.MapObjects
from p in mo.Points
select p).ToList();
for (int i = 0; i < test.Count(); i++)
{
test[i] = new Vector2(
test[i].X * map.Scale,
test[i].Y * map.Scale);
}
and this failed:
map.Objects.ForEach(l => l.MapObjects.ForEach(t => t.Points.ForEach(p => p = p * map.Scale)));
If I could get the dot notation variant working I would be very happy, but I do not have a clue on why it fails. Using the debugger it is obvious by examining the Points list that the vectors did not get multiplied using the two LINQ variants.
Update: Vector2 is a struct
Update: Here is two more one-liners that I found (working ones):
map.Objects.SelectMany(m => m.MapObjects).ToList().ForEach(o => o.Points = o.Points.Select(p => p * 2).ToList());
map.Objects.ForEach(l => l.MapObjects.ForEach(t => t.Points = t.Points.Select(p => p * 2).ToList()));