ascending/descending in LINQ - can one change the order via parameter?

后端 未结 4 888
时光取名叫无心
时光取名叫无心 2020-11-28 04:45

I have a method which is given the parameter \"bool sortAscending\". Now I want to use LINQ to create sorted list depending on this parameter. I got then this:



        
4条回答
  •  失恋的感觉
    2020-11-28 04:50

    What about ordering desc by the desired property,

       blah = blah.OrderByDescending(x => x.Property);
    

    And then doing something like

      if (!descending)
      {
           blah = blah.Reverse()
      }
      else
      {
          // Already sorted desc ;)
      }
    

    Is it Reverse() too slow?

提交回复
热议问题