I put raw data into a generic list. Then, I put data calculated from that raw data into another generic list, this one:
private List<ItemTotalsAcrossMonths> itemTotalsAcrossMonthsList;
The data (both raw and calculated) need to be displayed sorted, ordered by Total Purchases, so I want the data to be sorted that way. Since ordering a generic list returns another generic list, I first tried reassigning the sorted data into the original generic list:
itemTotalsAcrossMonthsList = (List<ItemTotalsAcrossMonths>)itemTotalsAcrossMonthsList.OrderByDescending(x =>
x.TotalPurchases13);
...but the road I'm on is apparently paved with good intentions, because this codeside bomb explodes in my face:
I tried putting the vals into a separate generic list of the same type:
private List<ItemTotalsAcrossMonths> itemTotalsAcrossMonthsList;
private List<ItemTotalsAcrossMonths> itemTotalsAcrossMonthsListSortedByTotalPurchasesDescending;
. . .
itemTotalsAcrossMonthsListSortedByTotalPurchasesDescending = (List<ItemTotalsAcrossMonths>)itemTotalsAcrossMonthsList.OrderByDescending(x => x.TotalPurchases13);
...but the results were eerily similar.
What do I need to do to sort the generic list?
You're missing ToList() call.
LINQ methods return
IEnumerable<T>
, notList<T>
, so you can't assume the implementation of interface is castable toList<T>
.You can call
ToList()
on LINQ query to get a list with the results.