Today I had to convert all the X values in a List
. Normally I would do something like the following (where theData is my source List and converter.Convert() is a method that converts to my desired units):
new_list = new List (theData.Count);
PointF new_point = new PointF();
foreach (PointF data_pt in theData)
new_point.X = (float)converter.Convert(data_pt.X);
new_point.Y = data_pt.Y;
After thinking about it for a bit and experimenting it turned out I could replace all the above code with a single LINQ statement.
new_list = (from d in theData
select new PointF((float) converter.Convert(d.X), d.Y)).ToList();
Now I just wish that Charlie had written his book a year ago (or maybe that I paid a little more attention when I was reading my first LINQ book).