Set the order of elements on FOREAS

My controller passes the list of objects to my view, allowing me to:

@foreach (var optiongroup in Model) { 

Inside the model is an IEnumerable<Option> . I need to sort this parameter list so that with I:

 @foreach (var option in optiongroup.Options){ 

As a result, I get a list in which the elements are sorted by the option.SortOrder property instead of the ordinal position of each element.

So how do I get a list sorted before foreach ? I tried:

 @foreach (var option in optiongroup.Options.OrderByDescending(o => optiongroup.SortOrder)) 

and

 IEnumerable<Option> allOptions = optiongroup.Options.OrderByDescending(o => optiongroup.SortOrder); // then foreaching the allOptions list 

but have not yet reached joy.

THX

+4
source share
1 answer

it

 IEnumerable<Option> allOptions = optiongroup.Options.OrderByDescending(o => optiongroup.SortOrder); 

it should be

 IEnumerable<Option> allOptions = optiongroup.Options.OrderByDescending(o => o.SortOrder); 
+7
source

All Articles