简体   繁体   中英

Custom Orderby/ThenBy

I'm not sure if this is possible, I'm trying to order/sort/rearrange a data:

  TYPE    NAME   CODE
     1     AAA      7
     1     BBB      8  
     5     CCC      6
     4     DDD      5
     1     EEE      7
     1     DDD      8

into like this:

  TYPE    NAME   CODE
     1     AAA      7
     1     EEE      7
     4     BBB      1
     5     CCC      6
     1     BBB      8 
     1     DDD      8

sorted by type then by name, but if code = 8 then it should be at last.

code:

 SomeList.OrderBy(c => c.Type).
  ThenBy(c => c.Name).
  ThenBy(c => Code);

You can use this "conditional sorting". It's important that the code check comes first:

var query = SomeList
    .OrderBy(c => c.Code == 8 ? 1 : 0)
    .ThenBy(c => c.Type)
    .ThenBy(c => c.Name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM