我试图在我的一个视图中实现一种排序机制,但它看起来OrderBy
无法处理“大”(更多见下文)整数字段,最终以混乱的顺序结束。
以下是所涉及的所有代码,尽可能精简:
楷模:
public class MyModel
{
public MyOption Option { get; set; }
}
public class MyOption
{
public int Value {get; set;}
public string Descr{get; set;}
}
显示模板\我的选项
@model MyOption
@Html.Label(Model.Description)
控制器的返回视图:
@model List<MyModel>
<ul>
@foreach(var row in Model)
{
<li>@Html.DisplayFor(x => row.Option)</li>
}
</ul>
控制器:
private List<MyModel> FakeRetrieveFromRepo()
{
return new List<MyModel>
{
new MyModel { Option = new MyOption { Value = 121370002, Descr = "One" }},
new MyModel { Option = new MyOption { Value = 121370000, Descr = "Two" }},
new MyModel { Option = new MyOption { Value = 121370001, Descr = "Three" }},
};
// Debugging confirms this order is kept
}
public ActionResult Index()
{
List<MyModel> data = FakeRetrieveFromRepo();
// "Offending" line: sorting is supposed to be done here
// neither .Option nor .Option.Value can be null
data = data.OrderBy(row => row.Option.Value).ToList();
// I expect the list to be sorted in this fashion
// [ {Option={ Value = 121370000, Descr = "Two" }},
// {Option={ Value = 121370001, Descr = "Three" }},
// {Option={ Value = 121370002, Descr = "One" }} ]
return View(data);
}
如果我尝试像这样排序,实际上不会进行排序,视图中的数据与存储库提供的数据顺序相同。如果我像这样更改该行,那么排序就会神奇地发生并给我预期的结果:
// If I change the line like this, sorting takes place!
data = data.OrderBy(row => row.Option.Value % 121370000).ToList();
我很习惯于实施晦涩难懂的变通方法来使东西正常工作,但看起来这里有一些我不知道的东西:似乎OrderBy
只能正确处理小数字?