IEnumerable <Object>数据特定排序

lov*_*iji 0 c# c#-4.0

我有一个包含属性ID的对象,其值介于101和199之间.如何订购它199,101,102 ... 198

结果我想把最后一项放到第一位.

Nuf*_*fin 7

所需的顺序没有任何意义(某些推理会有所帮助),但这应该可以解决问题:

int maxID = items.Max(x => x.ID); // If you want the Last item instead of the one
                                  // with the greatest ID, you can use
                                  // items.Last().ID instead.
var strangelyOrderedItems = items
    .OrderBy(x => x.ID == maxID ? 0 : 1)
    .ThenBy(x => x.ID);
Run Code Online (Sandbox Code Playgroud)

  • 而不是199,可能OP需要Max()或Last() (2认同)