Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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?
Posted
Updated 29-Dec-21 9:35am
v2
Comments
Sergey Alexandrovich Kryukov 7-Dec-12 15:53pm    
To make it reasonable easy to review, please provide definition of the ava_locationSet element type, and whatever else is involved. Make a complete compilable sample, simplify it down to bare minimum.
--SA
Zoltán Zörgő 7-Dec-12 15:55pm    
is it LINQ to Objects or LINQ against some database? In the later case your query is tried to be transformed to the native query language of that database engine, and that might be not possible. That driver might not know how to translate "parse".
Sergey Alexandrovich Kryukov 7-Dec-12 16:07pm    
...also, the purpose of Skip(0) is not clear. It does not skip anything, but even if it did (say, Skip(1)), it would skip from the element of the result set elements, not from the result set itself...
Otherwise, you sample works for strings, and to make it work on ava_location type, first show the definition of it...
--SA

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
 
Share this answer
 
Locquery = (DataServiceQuery<ava_location>)(from c in ctx.ava_locationSet
where (Convert.ToDouble(c.fn_Longitude) <= maxLon)
Select new ava_location
{
fn_Latitude = c.fn_Latitude,
fn_Longitude = c.fn_Longitude,
}).Skip(0).Take(MaxRows);
 
Share this answer
 
Comments
Maciej Los 29-Dec-21 15:48pm    
Do you think that OP is interested in an answer after aloms 10 years?
CHill60 30-Dec-21 4:47am    
It's always a good idea to provide some commentary rather than just dumping code. In this case all you have done is change Double.Parse to Convert.ToDouble - which will not solve the problem - Convert.ToDouble actually calls Double.Parse internally.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900