Wednesday, 2 October 2013

LINQ sorting of property List in List of custom object

LINQ sorting of property List in List of custom object

I have two lists of objects, each object has the property Recommendations
which itself is a list of 'Recommendation' objects. I want to sort the
recommendation objects based on one of its properties. I have come up with
this:
TPSqlORs.Where(x => x.Recommendations != null).ToList().ForEach(y =>
y.Recommendations.OrderBy(z => z.PointNumber));
SbmReportsORs.Where(x => x.Recommendations != null).ToList().ForEach(y =>
y.Recommendations.OrderBy(z => z.PointNumber));
But it causes no change at all to the original lists, which makes me
suspect the ToList() is just making a copy and the sorting is happening on
a copy which gets lost after execution. I searched along these lines but
apparently whilst it does make a copy, the new list contains references to
the original list elements, so surely it should be sorting them for both
lists?

1 comment: