2010-06-18 4 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

당신은 다른 방법의 쌍을 사용하여 하강 순서를 얻을 수 있습니다 : LINQ 쿼리를 사용

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

을, 당신은 쓸 수 있습니다 :

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

트릭은 당신이 하나의 orderby 절을 필요로한다는 것입니다 - 이들을 여러 개 추가하면 매번 목록이 다시 정렬됩니다. 다른 키를 사용하여 첫 번째 키가 동일한 요소를 정렬하려면 , (orderbyOrderBy 또는 OrderByDescending으로, ,ThenBy 또는 ThenByDescending으로 변환 됨)을 사용하여 요소를 구분해야합니다.

+0

대단히 감사합니다. 제 질문을 편집했습니다. 제발 좀보세요. –