2010-06-18 17 views
20
.OrderBy(y => y.Year).ThenBy(m => m.Month); 

Come impostare descending ordine?LINQ ordine decrescente multipla

EDIT:

ho provato questo:

var result = (from dn in db.DealNotes 
         where dn.DealID == dealID       
         group dn by new { month = dn.Date.Month, year = dn.Date.Year } into date 
         orderby date.Key.year descending 
         orderby date.Key.month descending 
         select new DealNoteDateListView { 
          DisplayDate = date.Key.month + "-" + date.Key.year, 
          Month = date.Key.month, 
          Year = date.Key.year, 
          Count = date.Count() 
         }) 
         //.OrderBy(y => y.Year).ThenBy(m => m.Month) 
         ; 

e sembra funzionare. È sbagliato usare lo orderby due volte come l'ho usato qui?

risposta

44

È possibile ottenere l'ordine decrescente per utilizzando una coppia diversa di metodi:

items.OrderByDescending(y => y.Year).ThenByDescending(m => m.Month); 

Usando query LINQ, è possibile scrivere:

from date in db.Dates 
orderby date.Key.year descending, date.Key.month descending 
select new { ... } 

Il trucco è che è necessario un solo orderby clausola - se ne aggiungi più di questi, l'elenco verrà riordinato ogni volta. Per ordinare gli elementi che hanno la prima chiave uguale utilizzando un'altra chiave, è necessario separare utilizzando , (orderby viene tradotto OrderBy o OrderByDescending mentre , viene tradotto ThenBy o ThenByDescending).

+0

fantastico, grazie :) Ho modificato la mia domanda, potresti per favore dare un'occhiata –

Problemi correlati