Conditional “orderby” sort order in LINQ

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

    You could try something like the following:

    var q = from i in list
             where i.Name = "name"
             select i;
    if(foo)
         q = q.OrderBy(o=>o.Name);
    else
         q = q.OrderByDescending(o=>o.Name);
    

提交回复
热议问题