字符串转换为int使用LINQ排序 [英] Convert string to int for ordering using LINQ

查看:1767
本文介绍了字符串转换为int使用LINQ排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用转换成一个int一个字符串命令我的名单:

I'd like to order my list by a string converted into an int:

var orderedListOfRfidTags = uow.RfidTags.OrderBy(t => Convert.ToInt32(t.Number)).ToList();

但得到的:不支持的方法ToInt32。

but get: The method 'ToInt32' is not supported.

推荐答案

我是光速的开发商之一。

I am one of the developers of LightSpeed.

在LINQ提供程序的光速3.11 RTM不支持Convert.ToInt32。然而,我们现在可以通过夜间的释放是可供下载现在增加的支持。

The LINQ provider in LightSpeed 3.11 RTM doesn't support Convert.ToInt32. However we have now added support via a nightly release which is available for download now.

如果你不想使用夜间的释放,你可以达到你想要的结果通过滴落到查询对象的API和直接调用SQL CAST功能。这将是这个样子:

If you don't want to use the nightly release, you can achieve the result you want by dropping down to the Query Objects API and invoking the SQL CAST function directly. This will look something like:

Query query = new Query
{
  Order = Order.By(Entity.Attribute("Number")
                         .Function("CAST", new LiteralExpression("INTEGER") { EmitInline = true }))
};

uow.Find<RfidTag>(query);

的原因相当冗长LiteralEx pression为铸造类型是默认光速通过参数(以避免SQL注入攻击)发送值到数据库。但对于CAST功能的SQL引擎需要看到 CAST(数,INTEGER),而不是 CAST(数字,@ P0)其中,p 0的值为整数。所以,你必须使用一个EmitInline EX pression,绕过参数化,而不是更自然的字符串。

The reason for the rather verbose LiteralExpression for the cast type is that by default LightSpeed sends values to the database through parameters (to avoid SQL injection attacks). But for the CAST function the SQL engine needs to see CAST(Number, INTEGER) rather than CAST(Number, @p0) where p0 has the value "INTEGER". So you have to use an EmitInline expression, which bypasses parameterisation, rather than the more natural string literal.

再次,虽然,夜间释放也支持Convert.ToInt32在LINQ所以你只需要,如果你想避免采取每晚构建砸下来到这个水平。

Once again, though, the nightly release does support Convert.ToInt32 in LINQ so you only need to drop down to this level if you want to avoid taking a nightly build.

这篇关于字符串转换为int使用LINQ排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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