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?
ToList().ForEach. TheToListcreates a fresh new collection from the first, so you are wasting cpu-cycles and even more memory just to be able to useList.ForEachinstead offoreach. In this case you're even using it more superfluously since you can chainOrderBydirectly behindWhere.Recommendations?List<Recommendation>