linq with entity order by desc

10,598

From the code I see that you return the query object to some external infrastructure (set DataSource property of a devexpress component). Then I'd day that this component just adds another .OrderBy to this query when it executes it object so your sorting is lost.

It seems to be very possible and logical since devexpress have their own sorting capabilities which seem to just override yours.

By setting DataSource you just provide a data set, and it is up to the component to add sorting, paging, etc.

So look at the component API in order to specify sorting that you need. Perhaps it has properties to do it.

Share:
10,598

Related videos on Youtube

user1958628
Author by

user1958628

Updated on June 04, 2022

Comments

  • user1958628
    user1958628 almost 2 years

    I just want to order descending like this:

    var query = from o in oEntite_T.ORDRE
                where o.DATE_CREE >= datedeb && o.DATE_CREE <= datefin
                orderby o.NO_ORDRE descending
                select o;
    

    It does not order descending, and I also tried:

    var query = (from o in oEntite_T.ORDRE
                 where o.DATE_CREE >= datedeb && o.DATE_CREE <= datefin
                 select  o).OrderByDescending(p => p.NO_ORDRE);
    

    I got the same result. Is this because the component Devexpress?

    linqServerModeSource_Ordre.KeyExpression = "NO_ORDRE;CODE_CLIENT";
    linqServerModeSource_Ordre.QueryableSource = oOrdre_BL.Get_OrdreEntity(dateEdit_Deb_Ordre.DateTime, dateEdit_Fin_Ordre.DateTime);
    
    gridControl_Liste_Ordres.DataSource = linqServerModeSource_Ordre;
    

    My complete code:

    public IQueryable<ORDRE> Get_OrdreEntity(DateTime datedeb, DateTime datefin)
    {
        try
        {
            IQueryable<ORDRE> LesListe;
            Soft8Exp_ClientEntities oEntite_T = new Soft8Exp_ClientEntities();
    
            var query = (from o in oEntite_T.ORDRE
                         where o.DATE_CREE >= datedeb && o.DATE_CREE <= datefin
                         select  o).OrderByDescending(p => p.NO_ORDRE);
            //var query = oEntite_T.ExecuteFunction<ORDRE>("qf").;
    
            LesListe = query;
            return LesListe;
        }
        catch (Exception excThrown)
        {
            throw new Exception("Err_02", excThrown);
        }
    }
    
    • Tom Styles
      Tom Styles over 10 years
      You need to debug what the dB query is returning to check where the order is being changed. Your code should return the items in the order you expected them.