Sorting decreasing!

1

In my cshtml page I have a foreach that reads this information from the database and plots on the screen in the form of a checkbox, I am trying to sort or reverse in descending order on the screen, but I am not getting it, it follows the information below the table data:

1 Amount of Water

2 Amount of Foam

3 Fire extinguisher

4 Sand / Land

5 Public

6 Private

7 Aceiro 8 Aircraft use

9 Mopper

11 Withdraw Material

I wanted to make my razor read from 11 to 1 to appear on the screen, someone could give me a help, follow the code:

@using SDO.Domain.Entities
@model List<OcorrenciaIncendioModoExtincao>
@{
    var modo1 = Model.FirstOrDefault(m => m.ModoExtincaoId == 1);
    var modo2 = Model.FirstOrDefault(m => m.ModoExtincaoId == 2);
    var modo5 = Model.FirstOrDefault(m => m.ModoExtincaoId == 5);
    var modo6 = Model.FirstOrDefault(m => m.ModoExtincaoId == 6);
}
<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">Modo Extinção</h4>
    </div>
    <div class="input-container panel-body" name="FormModoExtincao">
        <div class="col-sm-12">
            @foreach (var modo in ViewBag.ModoExtincao as List<ModoExtincao> )
            {
                if (modo.Id == 5)
                {
                    <div class="clearfix"></div>
                    <div class="col-sm-3">
                        
                    </div>
                }
                <div class="col-sm-3" name="[email protected]">
                    <p>
                        <label>
                            <input type="checkbox"
                                   classname="ModosExtincao"
                                   name="ModoExtincaoId"
                                   id="@modo.Id"
                                   value="@modo.Id"
                                   @(modo.Dependencia.HasValue ? "data-dep=" + modo.Dependencia.Value + " ignore" : "data-dep=0")
                                   @(Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Id) != null ? "checked" : "")
                                   @(modo.Dependencia.HasValue && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia) != null && ((modo.Id == 5 || modo.Id == 2) && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia).QtdConsumidaPublica > 0) ? "checked" : "" )
                                   @(modo.Dependencia.HasValue && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia) != null && (modo.Id == 6 && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia).QtdConsumidaParticular > 0) ? "checked" : "" ) />

                            @modo.Descricao
                        </label>
                        @if (modo.Id == 2 || modo.Id == 5 || modo.Id == 6) //ESPUMA, PUBLICA, PARTICULAR
                        {
                        <div class="complemento" data-dep="@modo.Id">
                            <span>@(modo.Id == 2 ? "Quantidade consumida (Litros)" : "Quantidade consumida (m³)")</span>
                            <div>
                                <input type="number"
                                       classname="ModosExtincao"
                                       class="form-control form-control-80"
                                       name="[email protected]"
                                       data-dep="@modo.Id"
                                       belongs-to="@(modo.Dependencia.HasValue ? modo.Dependencia : modo.Id)"
                                       searchfor="ModoExtincaoId"
                                       max="99999"
                                       maxlength="5"
                                       value="@((modo2 != null && modo.Id == 2 && modo2.QtdConsumidaEspuma != null)
	                                                    ? modo2.QtdConsumidaEspuma.ToString()
                                                            : (modo1 != null && modo.Id == 5 && modo1.QtdConsumidaPublica != null )
                                                            ? modo1.QtdConsumidaPublica.ToString()
                                                                : (modo1 != null && modo.Id == 6 && modo1.QtdConsumidaParticular != null)
                                                                    ? modo1.QtdConsumidaParticular.ToString()
                                                                    : "")" />
                            </div>
                            @if (modo.Id == 6)
                            {
                                var ed = Model.FirstOrDefault(m => m.ModoExtincaoId == modo.Dependencia);
                                <div>
                                    <span>Do próprio imóvel?</span>
                                    <label><input type="radio" name="ProprioImovel" value="1" @(ed != null && string.IsNullOrEmpty(ed.EnderecoOutraEdificacao) ? "checked" : "" ) />Sim</label>
                                    <label><input type="radio" name="ProprioImovel" value="0" @(ed != null && !string.IsNullOrEmpty(ed.EnderecoOutraEdificacao) ? "checked" : "" ) />Não</label>
                                    <div class="complemento">
                                        <span>Endereço da edificação da qual usou a água: </span>
                                        <input type="text"
                                               classname="ModosExtincao"
                                               class="form-control"
                                               belongs-to="@modo.Dependencia"
                                               searchfor="ModoExtincaoId"
                                               maxlength="80"
                                               name="EnderecoOutraEdificacao"
                                               value="@(ed != null ? ed.EnderecoOutraEdificacao : "" )" />
                                    </div>
                                </div>
                            }
                        </div>
                        }
                    </p>&nbsp;
                </div>
            }
        </div>
    </div>
</div>

Thank you.

    
asked by anonymous 21.12.2018 / 18:32

1 answer

1

As discussed in the comments, you would like to sort by decreasing the identifier of your objects. To do this, change the line of your foreach as shown below, so we will perform the ordering via Linq :

@foreach (var modo in (ViewBag.ModoExtincao as List<ModoExtincao>).OrderByDescending(o => o.Id))
{
    //Aqui continua o fluxo já existente...
}

If you want to add more fields to the sort, use the ThenByDescending method for descending ordering and ThenBy for sort ordering. You can use as many times as needed:

@foreach (var modo in (ViewBag.ModoExtincao as List<ModoExtincao>).OrderByDescending(o => o.Id).ThenByDescending(o => o.Descricao).ThenByDescending(o.Dependencia))
{
    //Aqui continua o fluxo já existente...
}
    
21.12.2018 / 18:48