1

I have this situation: I need to find nearby location using lat and long. Using this:

var coord = new GeoCoordinate(latitude, longitude);
                var nearest = context.ParkingSpaces.Select(x => new GeoCoordinate(x.Latitude.Value, x.Longitude.Value))
                                         .OrderBy(x => x.GetDistanceTo(coord)).First();

But I am getting following error:

Only parameterless constructors and initializers are supported in LINQ to Entities

Please help

  • 4
    Possible duplicate of [Only parameterless constructors and initializers are supported in LINQ to Entities](http://stackoverflow.com/questions/3571084/only-parameterless-constructors-and-initializers-are-supported-in-linq-to-entiti) – Ivan Stoev Feb 28 '16 at 17:50
  • Yes. I visited that page. But couldn't find proper solution for GeoCoordinate – Chirag Gardhariya Feb 28 '16 at 17:59

1 Answers1

0

You can rewrite to something like this:

var coord = new GeoCoordinate(latitude, longitude);
var nearest = context.ParkingSpaces
     .Select(x => new { Latitude = x.Latitude.Value, Longitude = x.Longitude.Value })
     .ToList()
     .Select(x => new GeoCoordinate(Latitude, Longitude))
     .OrderBy(x => x.GetDistanceTo(coord))
     .First();
aleha_84
  • 8,309
  • 2
  • 38
  • 46