2016-08-01 51 views
0

订购我似乎遇到了问题排序作为其唯一的排序第一位LINQ的只是第一个数字

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(); 

任何帮助吗?

+1

您只选择具有特定“StorageCapacity”的项目,然后按该值排序。这是毫无意义的,因为所有的项目都会有相同的值,所以你会得到一个任意的顺序。 –

+0

根据@Tim的评论,我假设你在问'else'的情况。什么是'StorageCapacity'的类型?此外,使用“ToListAsync”并立即“等待”结果的意义何在? – slawekwin

+0

public int StorageCapacity {get;组; } - 这是一个int – momo003

回答

1

在某个位置,您的StorageCapacity是一个字符串。

看这个代码(直播例如: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))); 

的输出是:

1000,500,0
500,1000,0

第一个结果是你期望的order ints降序。第二个是你从同样的字符串输入中得到的。