1

I'm trying to retrieve data in a specific order (to establish a ranking) with the below code:

public ActionResult ShowRanking(int id = 0)
        {
            Tournament tournament = db.Tournament.Find(id);

            var parti = (from p in db.Participe
                        where p.IdTournament == tournament.IdTournament
                        //orderby p.ArchTotalScore descending
                        select p).OrderByDescending(x => x.ArchTotalScore);

            //objlist = parti;

            foreach (var part in parti)
            {
                tournament.Participe.Add(part);

                //objlist.Add(part);

            }
            tournament.Participe.OrderByDescending(x => x.ArchTotalScore);

            return View(tournament.Participe);
        }

The data is retrieved but whenever I pass the list ofdata to my view, the order criterias I used are ignored and the records are shown as they have been inserted in the DB.

Here is my view as well:

@model ArcheryComp.Tournament

@{
    ViewBag.Title = "Classement";
}

<h2>Classement</h2>

    <table>
        <tr>
            <th>
                Nom
            </th>
            <th>
                Prenom
            </th>
            <th>
                Division
            </th>
            <th>
                Categorie
            </th>
            <th>
                Score 1
            </th>
            <th>
                Score 2
            </th>
            <th>
                Total Score
            </th>
        </tr>
    @foreach (var item in Model.Participe)
    {

                    <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Archers.Nom)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Archers.Prenom)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Divisions.DivDescription)
                    </td>
                     <td>
                        @Html.DisplayFor(modelItem => item.Categorie)
                    </td>
                        <td>
                        @Html.DisplayFor(modelItem => item.ArchScore1)
                    </td>
                        <td>
                        @Html.DisplayFor(modelItem => item.ArchScore2)
                    </td>
                        <td>
                        @Html.DisplayFor(modelItem => item.ArchTotalScore)
                    </td>
                    <br />
                </tr>
                var tmp = item.IdTournament;
            }

    </table>

Do you have any idea what is wrong ? and could I correct this ?

Thanks in advance for your help.

5
  • tournument.participate is what?list,hashset Commented Aug 14, 2015 at 14:47
  • 1
    You need to set your result to the result of OrderByDescending and return that. Commented Aug 14, 2015 at 14:49
  • 1
    @dustmouse is right, OrderByDescending doesnt modify the original list, it only returns a new ordered version. Commented Aug 14, 2015 at 14:49
  • Thanks for your feedback all !! @dustmouse how can I set my results to the results of OrderByDescending? Commented Aug 14, 2015 at 14:59
  • @DavidE, Are you using Entity Framework? Commented Aug 14, 2015 at 15:06

1 Answer 1

1

You ordered the tournament.Participe, but you did not use the result. You must change it to something like this(if tournament.Participe is a Listofcourse):

tournament.Participe = 
       tournament.Participe.OrderByDescending(x => x.ArchTotalScore).ToList();
return View(tournament);

The model used in the view is ArcheryComp.Tournament therefore you must return View(tournament) not View(tournament.Participe).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.