<分区>
我有一些数据来自数据库,我无法轻易更改其架构。我想对其进行排序并将其绑定(bind)到基于数字 ID 的控件。问题是 API 将数字存储在字符串字段中,而不是作为 int
和 Linq barfs 进行转换尝试。
myControl.DataSource = dataFromDB.OrderBy(o => int.Parse(o.StringHoldingAnInt));
LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.
Convert.ToInt32
也不起作用。
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
作为字符串排序是不合适的,因为值的长度并不完全相同,它会像这样对它们进行排序:1, 10, 11, 2, 3..