Linq将String比较为Double [英] Linq Compare String to Double

查看:181
本文介绍了Linq将String比较为Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个LINQ查询,该查询接受一个字符串值列表(包含数字)并检查 maxLon (一个Double变量)



示例:

How to create a LINQ query that takes a list of string values (that has numbers) and check it against a maxLon (a Double variable)

Example:

Locquery = (DataServiceQuery<ava_location>)(from c in ctx.ava_locationSet 
                                            where (Double.Parse(c.fn_Longitude) <= maxLon)
                                            Select new ava_location
                                            {
                                               fn_Latitude = c.fn_Latitude,
                                               fn_Longitude = c.fn_Longitude,
                                            }).Skip(0).Take(MaxRows);





这个编译,但在执行时,我得到以下异常:



This compiles, but upon execution, I got the following exception:

System.NotSupportedException was unhandled by user code
  Message=The expression ((((Parse([10007].fn_Latitude) >= 24.7614342656583) And (Parse([10007].fn_Latitude) <= 24.7686757343417)) And (Parse([10007].fn_Longitude) >= -80.0667877343417)) And (Parse([10007].fn_Longitude) <= -80.0595462656583)) is not supported.





如何使用数字的字符串值来大于或小于?



How can I use a string value of numbers to do greater than, or less than?

推荐答案

第一个选项尝试将varchar / string类型转换为数据库表中的double。第二个选项是加载所有没有过滤器的记录集,并在内存中进行比较,这将严重影响性能。第三个选项增强LinqToSql以支持Double.Parse将sql转换为类似Cast(列名为double)。



请参考 http://stackoverflow.com/questions/5971521/linq-to-entities-does -not-recognition-the-method-double-parsesystem-string-met [ ^ ]

虽然这是针对实体框架的
first option try to convert the varchar / string type to double in database table. Second option is load all recordset without filter and compare in memory which will hit performance badly. Third option enhance LinqToSql to support Double.Parse to convert sql something like Cast(columnname as double) .

please refer http://stackoverflow.com/questions/5971521/linq-to-entities-does-not-recognize-the-method-double-parsesystem-string-met[^]
although this is for entity framework


这篇关于Linq将String比较为Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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