Conditional “orderby” sort order in LINQ

前端 未结 9 1279
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 23:28

In LINQ, is it possible to have conditional orderby sort order (ascending vs. descending).

Something like this (not valid code):

bool flag;

(from w          


        
9条回答
  •  一生所求
    2020-12-06 00:03

     bool flag;
    
     from w in widgets
     where w.Name.Contains("xyz")
     orderby flag == true ? w.Id : w.Id descending
     select w
    

    ascending is impled

提交回复
热议问题