0

I have this entity code that is giving me issues, it doens't give an error until I load the page and then the error says. LINQ to Entities does not recognize the method 'Double Acos(Double)' method, and this method cannot be translated into a store expression I am doing the haversine function to get the radius of a point, I know that Math returns a Double thats why I have cast it and I have to use floats as thats the only format the CSV files are in, how can i correct the above error?

 var ste = (from s in db.zipss where Math.Acos(Math.Sin(28.46348)
 * Math.Sin((float)s.latitude) + Math.Cos(28.46348) * Math.Cos((float)s.longitude - 
(-81.3881))) * 3960 <= 5 select s.zipcode).FirstOrDefault();
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
user1949387
  • 1,245
  • 3
  • 21
  • 38
  • 1
    create a variable, set the value to your math, and then use the variable instead. [Duplicate](http://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method) – MikeSmithDev Feb 17 '13 at 22:19

1 Answers1

0

The expression can't be translated, for LINQ to Entities. Since no other solutions have been proposed, I guess you can always do the query in memory, i.e. go to LINQ to Objects, as in:

var ste = (from s in db.zipss.AsEnumerable()  // note AsEnumerable
           where Math.Acos(Math.Sin(28.46348) * Math.Sin((float)s.latitude)
               + Math.Cos(28.46348) * Math.Cos((float)s.longitude - (-81.3881)))
               * 3960 <= 5
           select s.zipcode).FirstOrDefault();

I don't know if this will be too slow to be practical. I don't know about the casts to float. Maybe you can omit them (if .latitude and .longitude are already float, a float can implicitly promote to a double).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181