momo003
momo003

Reputation: 48

Linq only ordering by first digit

I seem to be having trouble sorting as its only sorting the first digit

int storageCapacity;

IEnumerable<ProductStorageCapacity> items;

if (int.TryParse(filter["StorageCapacity"], out storageCapacity) && storageCapacity > 0)
     items = await context.ProductStorageCapacities
     .Where(ps => ps.StorageCapacity == storageCapacity)
     .OrderByDescending(ps => ps.StorageCapacity).ToListAsync();
else items = await context.ProductStorageCapacities
     .OrderBy(ps => ps.StorageCapacity).ToListAsync();

Any help?

Upvotes: 0

Views: 229

Answers (1)

Jamiec
Jamiec

Reputation: 136104

Somewhere down the line, your StorageCapacity is a string.

Look at this code (Live example: http://rextester.com/NZALE31796):

var intList = new []{0,500,1000};
var strList = new []{ "0", "500", "1000"};


Console.WriteLine(String.Join(",",intList.OrderByDescending(x => x)));
Console.WriteLine(String.Join(",",strList.OrderByDescending(x => x)));

The output is:

1000,500,0
500,1000,0

The first result is what you would expect ordering ints descending. The second is what you get from the same input of strings.

Upvotes: 1

Related Questions