Orderby() 没有正确排序数字 c#
- 作者: 嗫?暁雲?
- 来源: 51数据库
- 2022-12-13
问题描述
我正在为我的公司编写一个应用程序,目前正在研究搜索功能.当用户搜索一个项目时,我想显示最高版本(存储在数据库中).
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
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
