在我的 C# 应用程序中,我有一个对象集合,这些对象的 int Order
属性范围从 1 到 n。
当我这样做时:
var listings = session.Query<Listing>().Where(x => !x.IsDeleted && x.CategoryId == category.Id && x.WorkflowStatus == WorkflowStatus.Published).OrderBy(x => x.Order);
我得到了一系列列表,但不是 100% 的顺序正确。按照目前的顺序:
0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 28, 29, 3, 30, 31, 32, 33, 4 ....
知道为什么 OrderBy
没有完全按预期执行吗?
最佳答案
如果您使用索引,则需要为 Order 属性设置排序选项。 来自 http://ravendb.net/docs/client-api/querying/static-indexes/customizing-results-order
Numerical values, on the other hand, are stored as text and therefore require the user to specify explicitly what is the number type used so a correct sorting mechanism is enforced. This is quite easily done, by declaring the required sorting setup in SortOptions in the index definition:
Sort(x => x.Order, SortOptions.Int);
The index outlined above will allow sorting by value on the user's age (1, 2, 3, 11, etc). If we wouldn't specify this option, it would have been sorted lexically (1, 11, 2, 3, etc). The default SortOptions value is String. Appropriate values available for all numeric types (Byte, Double, Float, Int, Long and Short).
关于c# - RavenDB 排序方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16914485/