Orderby()没有正确地命令编号c#。

时间:2021-07-27 22:48:43

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).

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

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

问题是,该版本以字符串的形式存储,而不是int,当我在结果上执行OrderBy(q=>q.Version)时,它们会被返回。

1
10
11
2
3
...

Obviously 2 comes before 10.

显然2在10之前。

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.

有没有办法让我把这个版本转换成整数,或者有一个简单的IComparer ?到目前为止我找不到任何实质性的东西。

I tried doing this:

我试着这样做:

var items = (from r in results
             select r).OrderBy(q => Int32.Parse(q.Version));

This compiles but doesn't work.

这将编译,但不起作用。

12 个解决方案

#1


25  

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

Int32。LinqToSql翻译不支持解析。转换。ToInt32支持。

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

http://msdn.microsoft.com/en-us/library/bb882655.aspx

http://msdn.microsoft.com/en-us/library/bb882655.aspx

#2


7  

Your problem is somewhere else, the following works:

你的问题在别处,下面的工作是:

new[] { "1", "10", "2", "3", "11" }
    .OrderBy(i => int.Parse(i))
    .ToList()
    .ForEach(Console.WriteLine);

If your problem is LINQ to SQL, then what is happening is CLR is trying to create SQL out of your LINQ and doesn't understand int.Parse. What you can do is first get the data from SQL then order it once all data is loaded:

如果您的问题是LINQ to SQL,那么正在发生的是CLR试图从您的LINQ中创建SQL,并且不理解int.Parse。你能做的是先从SQL获取数据,然后在加载所有数据后对其进行排序:

var items = (from r in results
             select r)
            .ToList()
            .OrderBy(q => Int32.Parse(q.Version));

Should do it.

应该这样做。

#3


6  

If you're unable to change your table definition (so the version is a numeric type), and your query really is as listed (your not using skip, or take, or otherwise reducing the number of results), the best you can do is call "ToList" on the unsorted results, which when you then apply an OrderBY lambda to it will take place in your code, rather than trying to do it at the SQL Server end (and which should now work).

如果你无法改变你的表定义(版本是一个数字类型),和您的查询是上市(不使用跳过,或者,或者减少结果)的数量,最好你能做的就是叫“ToList”未排序的结果,然后当你申请一个OrderBYλ将在您的代码,而不是试图在SQL服务器端(现在应该工作)。

#4


5  

Why are you sorting in a lambda? Why don't you just sort in the query?

你为什么要选一个?为什么不在查询中排序呢?

var query = from r in items
            orderby int.Parse( r )
            select r;

Now that we know you are using LINQ to SQL, you might consider making a standard SQL call on this one by doing something like:

既然我们知道您正在使用LINQ to SQL,那么您可以考虑使用以下方法来进行标准SQL调用:

Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...

Or even

甚至

Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...
Order By Cast( TextWhichShouldBeIntCol As int )

That will bleed into your LINQ as an int (and if you use the second iteration, be ordered). That avoids having to go through the resultset twice in LINQ (once for querying, once for ordering).

这将在您的LINQ中作为int(如果您使用第二次迭代,被命令)流到您的LINQ中。这样就避免了在LINQ中两次检查结果(一次查询一次,一次用于排序)。

#5


5  

There's an awesome piece of code that does a great job when it comes to natural sorting. Its name is AlphanumComparator.

当涉及到自然排序时,有一段很棒的代码做得很好。它的名字叫AlphanumComparator。

Sample code:

示例代码:

var ordered = Database.Cars.ToList().OrderBy(c => c.ModelString, new AlphanumComparator());

Note that the list must be in memory.

注意,列表必须在内存中。

If you get the C# version, do this:

如果你得到c#版本,那么做这个:

AlphanumComparator : IComparer<string>

and

public int Compare(string x, string y)

#6


1  

I made a test. I have the following code.

我做了一个测试。我有以下代码。

string[] versions = { "1", "2", "10", "12", "22", "30" };
foreach (var ver in versions.OrderBy(v => v))
{
     Console.WriteLine(ver);
}

As expected the result is 1, 10, 12, 2, 22, 30 Then lets change versions.OrderBy(v => v)) to versions.OrderBy(v => int.Parse(v))). And it works fine: 1, 2, 10, 12, 22, 30

正如预期的那样,结果是1、10、12、2、22、30,然后改变版本。OrderBy(v => v))到版本。OrderBy(v = > int.Parse(v)))。它很好用:12 10 12 22 30。

I think your problem is that you have nondigit chars in your string like '.'. What kind of exception do you get?

我想你的问题是你的字符串中有非数字字符。你有什么例外?

#7


1  

try this:

试试这个:

var items = results.(Select(v => v).OrderBy(v => v.PadLeft(4));

that'll work in Linq2Sql

会在Linq2Sql工作

#8


1  

Why are you sorting if you only need "the highest version"? It sounds like you could avoid some overhead if you used Max().

如果只需要“最高版本”,为什么还要排序?如果使用Max(),您可以避免一些开销。

Also, you really should change the column type to integer.

另外,您确实应该将列类型改为integer。

#9


1  

var items = (from r in results
         select r).OrderBy(q => Convert.ToInt32(q.Version));

Definitely run......

肯定跑……

#10


0  

It sounds like you have a text value instead of a numeric value.

这听起来好像你有一个文本值而不是数值。

If you need to sort, you can try:

如果你需要排序,你可以试试:

var items = (from r in results
             select r);
return items.OrderBy( v=> Int.Parse(v.Version) );

#11


0  

var query = from r in items
            let n = int.Parse(r)
            orderby n
            select n;

#12


0  

var items = (from v in results
                    select v).ToList().OrderBy(x => int.Parse(x.Version));

#1


25  

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

Int32。LinqToSql翻译不支持解析。转换。ToInt32支持。

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

http://msdn.microsoft.com/en-us/library/bb882655.aspx

http://msdn.microsoft.com/en-us/library/bb882655.aspx

#2


7  

Your problem is somewhere else, the following works:

你的问题在别处,下面的工作是:

new[] { "1", "10", "2", "3", "11" }
    .OrderBy(i => int.Parse(i))
    .ToList()
    .ForEach(Console.WriteLine);

If your problem is LINQ to SQL, then what is happening is CLR is trying to create SQL out of your LINQ and doesn't understand int.Parse. What you can do is first get the data from SQL then order it once all data is loaded:

如果您的问题是LINQ to SQL,那么正在发生的是CLR试图从您的LINQ中创建SQL,并且不理解int.Parse。你能做的是先从SQL获取数据,然后在加载所有数据后对其进行排序:

var items = (from r in results
             select r)
            .ToList()
            .OrderBy(q => Int32.Parse(q.Version));

Should do it.

应该这样做。

#3


6  

If you're unable to change your table definition (so the version is a numeric type), and your query really is as listed (your not using skip, or take, or otherwise reducing the number of results), the best you can do is call "ToList" on the unsorted results, which when you then apply an OrderBY lambda to it will take place in your code, rather than trying to do it at the SQL Server end (and which should now work).

如果你无法改变你的表定义(版本是一个数字类型),和您的查询是上市(不使用跳过,或者,或者减少结果)的数量,最好你能做的就是叫“ToList”未排序的结果,然后当你申请一个OrderBYλ将在您的代码,而不是试图在SQL服务器端(现在应该工作)。

#4


5  

Why are you sorting in a lambda? Why don't you just sort in the query?

你为什么要选一个?为什么不在查询中排序呢?

var query = from r in items
            orderby int.Parse( r )
            select r;

Now that we know you are using LINQ to SQL, you might consider making a standard SQL call on this one by doing something like:

既然我们知道您正在使用LINQ to SQL,那么您可以考虑使用以下方法来进行标准SQL调用:

Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...

Or even

甚至

Select ..., Cast( TextWhichShouldBeIntCol As int ) As IntCol
From ...
Order By Cast( TextWhichShouldBeIntCol As int )

That will bleed into your LINQ as an int (and if you use the second iteration, be ordered). That avoids having to go through the resultset twice in LINQ (once for querying, once for ordering).

这将在您的LINQ中作为int(如果您使用第二次迭代,被命令)流到您的LINQ中。这样就避免了在LINQ中两次检查结果(一次查询一次,一次用于排序)。

#5


5  

There's an awesome piece of code that does a great job when it comes to natural sorting. Its name is AlphanumComparator.

当涉及到自然排序时,有一段很棒的代码做得很好。它的名字叫AlphanumComparator。

Sample code:

示例代码:

var ordered = Database.Cars.ToList().OrderBy(c => c.ModelString, new AlphanumComparator());

Note that the list must be in memory.

注意,列表必须在内存中。

If you get the C# version, do this:

如果你得到c#版本,那么做这个:

AlphanumComparator : IComparer<string>

and

public int Compare(string x, string y)

#6


1  

I made a test. I have the following code.

我做了一个测试。我有以下代码。

string[] versions = { "1", "2", "10", "12", "22", "30" };
foreach (var ver in versions.OrderBy(v => v))
{
     Console.WriteLine(ver);
}

As expected the result is 1, 10, 12, 2, 22, 30 Then lets change versions.OrderBy(v => v)) to versions.OrderBy(v => int.Parse(v))). And it works fine: 1, 2, 10, 12, 22, 30

正如预期的那样,结果是1、10、12、2、22、30,然后改变版本。OrderBy(v => v))到版本。OrderBy(v = > int.Parse(v)))。它很好用:12 10 12 22 30。

I think your problem is that you have nondigit chars in your string like '.'. What kind of exception do you get?

我想你的问题是你的字符串中有非数字字符。你有什么例外?

#7


1  

try this:

试试这个:

var items = results.(Select(v => v).OrderBy(v => v.PadLeft(4));

that'll work in Linq2Sql

会在Linq2Sql工作

#8


1  

Why are you sorting if you only need "the highest version"? It sounds like you could avoid some overhead if you used Max().

如果只需要“最高版本”,为什么还要排序?如果使用Max(),您可以避免一些开销。

Also, you really should change the column type to integer.

另外,您确实应该将列类型改为integer。

#9


1  

var items = (from r in results
         select r).OrderBy(q => Convert.ToInt32(q.Version));

Definitely run......

肯定跑……

#10


0  

It sounds like you have a text value instead of a numeric value.

这听起来好像你有一个文本值而不是数值。

If you need to sort, you can try:

如果你需要排序,你可以试试:

var items = (from r in results
             select r);
return items.OrderBy( v=> Int.Parse(v.Version) );

#11


0  

var query = from r in items
            let n = int.Parse(r)
            orderby n
            select n;

#12


0  

var items = (from v in results
                    select v).ToList().OrderBy(x => int.Parse(x.Version));