排序依据()无法正常订购号C# [英] Orderby() not ordering numbers correctly c#

查看:88
本文介绍了排序依据()无法正常订购号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).

问题是,该版本被存储为一个字符串,而不是整型,当我做对结果的排序依据(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));

这编译,但不起作用。

推荐答案

Int32.Parse不受LinqToSql翻译支持。 Convert.ToInt32是支持的。

Int32.Parse is not supported by the LinqToSql translator. Convert.ToInt32 is supported.

<一个href=\"http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx\">http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

<一个href=\"http://msdn.microsoft.com/en-us/library/bb882655.aspx\">http://msdn.microsoft.com/en-us/library/bb882655.aspx

这篇关于排序依据()无法正常订购号C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆