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

descending ऑर्डर कैसे सेट करें?linq एकाधिक ऑर्डर DESCENDING

संपादित:

मैं इस कोशिश की:

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) 
         ; 

और यह काम कर लगता है। क्या orderby का उपयोग करना गलत है जैसे मैंने इसे यहां इस्तेमाल किया?

उत्तर

44

आप तरीकों की एक अलग जोड़ी का उपयोग करके उतरते आदेश प्राप्त कर सकते हैं:

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

LINQ क्वेरी का उपयोग करना, आप लिख सकते हैं:

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

चाल कि आप केवल एक orderby खंड की जरूरत है - यदि आप इनमें से कई जोड़ते हैं, तो सूची हर बार फिर से क्रमबद्ध की जाएगी। उन तत्वों को सॉर्ट करने के लिए जिनके पास दूसरी कुंजी का उपयोग करने वाली पहली कुंजी है, आपको , (orderby का उपयोग करके OrderBy या OrderByDescending पर अनुवादित किया गया है जबकि , का अनुवाद ThenBy या ThenByDescending) में किया गया है।

+0

महान, धन्यवाद :) मैंने अपना प्रश्न संपादित किया, क्या आप कृपया एक नज़र डालें –

संबंधित मुद्दे