Orderby() 没有正确排序数字 c#

Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
本文介绍了Orderby() 没有正确排序数字 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在为我的公司编写一个应用程序,目前正在研究搜索功能.当用户搜索一个项目时,我想显示最高版本(存储在数据库中).

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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
How to store delegates in a List(如何将代表存储在列表中)
How delegates work (in the background)?(代表如何工作(在后台)?)
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)