1

I am trying to order my list cards but push the zeros to the end. I read a few responses on this here and tried the solution, but it doesn't seem to work.

Please see my linq query below. (ToPackCards is just an extension to map to my custom object)

List<PackCard> packCards = db
  .PackCardEntities
  .OrderBy(pc => pc.PackNumber)
  .ThenBy(pc => pc.PickPosition == 0)
  .ThenBy(pc => pc.PickPosition)
  .ToList()
  .ToPackCards();

My data looks like this

Id|PackNumber|PickPosition|CardId

1|1|null|100 
2|1|null|120  
3|1|1|134  
4|1|2|232  
5|1|null|456

I want this to be sorted like this

3|1|1|134
4|1|2|232
1|1|null|100
2|1|null|120
5|1|null|456

What I get is this

1|1|null|100
2|1|null|120
5|1|null|456
3|1|1|134
4|1|2|232

Let me know if I am messing this up somehow, thanks!

Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
Wil
  • 2,328
  • 2
  • 20
  • 27

2 Answers2

1

Try this one

List<PackCard> packCards = db.PackCardEntities
  .OrderBy(pc => pc.PackNumber)
  .ThenBy(pc => pc.PickPosition.HasValue? pc.PickPosition : int.MaxValue)
  .ToList().ToPackCards();
Primary Key
  • 1,237
  • 9
  • 14
1
List<PackCard> packCards = db 
  .PackCardEntities 
  .OrderBy(pc => pc.PackNumber) 
  .ThenBy(pc => pc.PickPosition == null) 
  .ThenBy(pc => pc.PickPosition) 
  .ToList() 
  .ToPackCards(); 
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61