Linq OrderBy does not sort a List. How do I sort the list?

前端 未结 5 1740
感动是毒
感动是毒 2020-12-04 02:24

Having issues with the OrderBy clause not having any impact on the sort. I have walked through this in the debugger and insuring this is a case that the sort line of the cod

5条回答
  •  孤街浪徒
    2020-12-04 02:49

    OrderBy returns a query that would perform the ordering: it does not modify the original list (whereas something like List.Sort would modify the original)

    Instead try something like:

    ddlOptions = ddlOptions.OrderBy(l => l.DisplayText).ToList();
    

    EDIT: You might want to play around with the type of ddlOptions or where/how you return the data as we're doing an extra ToList than probably necessary, but that's probably a minor/non-issue for this case anyway.

提交回复
热议问题