oumaima
oumaima

Reputation: 71

Linq-OrderByDescending doesn't work for me-ASP.net MVC

I want to order my list by idEtatD but this attribute isn't my table primarykey or id it's a normal attribute migrated from another table,nut OrderBy or OrderByDescending didn't give me a result my list still not ordered by idEtatD.

 public ActionResult ListeDemande( int? page)
        {


            traçabilitérepository=new TraçabilitéDemandeRepository(db);

            var listdemandes = (from d in db.Demande_Gabarit
                                join t in db.Traçabilité_Demande_Gabarit
                                    on d.id_demande equals t.iddemande into ThisList
                                from t in ThisList.DefaultIfEmpty()

                                select new
                                {
                                    id_demande=d.id_demande,
                                    NumDemande = d.NumDemande,
                                    Emetteur = d.Emetteur,
                                    Date = d.Date,
                                    Ligne = d.Ligne.designation,
                                    Etat = t.Etat_Demande_Gabarit.EtatDemande

                                }).ToList().Select(x => new DemandeViewModel()
                                {
                                    NumDemande = x.NumDemande,
                                    Emetteur = x.Emetteur,
                                    Date = x.Date,
                                    designation = x.Ligne,
                                    EtatDemande = x.Etat,
                                    id_demande = x.id_demande
                                });
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(listdemandes.OrderByDescending(x => x.idEtatD).ToList().ToPagedList(pageNumber, pageSize));
        }

Please I need your help and thank you.

Upvotes: 2

Views: 140

Answers (1)

Krom
Krom

Reputation: 98

You can order the items at the beginning, but you need to include it in the list:

traçabilitérepository = new TraçabilitéDemandeRepository(db);

            var listdemandes = (from d in db.Demande_Gabarit
                                join t in db.Traçabilité_Demande_Gabarit
                                    on d.id_demande equals t.iddemande into ThisList
                                from t in ThisList.DefaultIfEmpty()
                                orderby t.idEtatD descending

                                select new
                                {
                                    id_demande = d.id_demande,
                                    NumDemande = d.NumDemande,
                                    Emetteur = d.Emetteur,
                                    Date = d.Date,
                                    Ligne = d.Ligne.designation,
                                    Etat = t.Etat_Demande_Gabarit.EtatDemande,
                                    idEtatD = XXXX
                                }).ToList().Select(x => new DemandeViewModel()
                                {
                                    NumDemande = x.NumDemande,
                                    Emetteur = x.Emetteur,
                                    Date = x.Date,
                                    designation = x.Ligne,
                                    EtatDemande = x.Etat,
                                    id_demande = x.id_demande
                                });

Upvotes: 2

Related Questions