Conditional “orderby” sort order in LINQ

前端 未结 9 1298
爱一瞬间的悲伤
爱一瞬间的悲伤 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:15

    ...Or do it all in one statement

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

提交回复
热议问题