問題描述
我正在為我的公司編寫一個應用程序,目前正在研究搜索功能.當用戶搜索一個項目時,我想顯示最高版本(存儲在數據庫中).
I am writing an app for my company and am currently working on the search functionality. When a user searches for an item, I want to display the highest version (which is stored in a database).
問題是,版本存儲為字符串而不是整數,當我對結果執行 OrderBy(q=>q.Version) 時,它們會像
The problem is, the version is stored as a string instead of int, and when I do an OrderBy(q=>q.Version) on the results, they are returned like
1
10
11
2
3
...
顯然 2 在 10 之前.
Obviously 2 comes before 10.
有沒有辦法讓我將版本轉換為整數,或者是否有一個簡單的 IComparer?到目前為止,我找不到任何實質性的東西.
Is there a way for me to cast the version as an integer or is there a simple IComparer out there? I couldn't find anything substantial thus far.
我嘗試這樣做:
var items = (from r in results
select r).OrderBy(q => Int32.Parse(q.Version));
這可以編譯但不起作用.
This compiles but doesn't work.
推薦答案
LinqToSql 轉換器不支持 Int32.Parse.支持 Convert.ToInt32.
Int32.Parse is not supported by the LinqToSql translator. Convert.ToInt32 is supported.
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
http://msdn.microsoft.com/en-us/library/bb882655.aspx
這篇關于Orderby() 沒有正確排序數字 c#的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!