Reputation: 21430
I am trying to get the closest location via my LINQ query:
var coord = new GeoCoordinate(loc.Latitude, loc.Longitude);
var nearest = ctx.Locations
.Select(x => new LocationResult {
location = x,
coord = new GeoCoordinate(x.Latitude.GetValueOrDefault(),
x.Longitude.GetValueOrDefault())
})
.OrderBy(x => x.coord.GetDistanceTo(coord))
.First();
return nearest.location.Id;
However, I am getting the following error:
Only parameterless constructors and initializers are supported in LINQ to Entities.
I have tried Googling this but I am still not sure how to fix it. What is a parameterless constructor?
Upvotes: 1
Views: 2040
Reputation: 22794
You need to try this instead:
var coord = new GeoCoordinate(loc.Latitude, loc.Longitude);
var nearest = ctx.Locations
.Select(x => new LocationResult {
location = x,
coord = new GeoCoordinate { Latitude = x.Latitude ?? 0, Longitude = x.Longitude ?? 0 }
})
.OrderBy(x => x.coord.GetDistanceTo(coord))
.First();
return nearest.location.Id;
Upvotes: 2
Reputation: 46750
The problem is this line
new GeoCoordinate(x.Latitude.GetValueOrDefault(), x.Longitude.GetValueOrDefault())
This is using a constructor with a parameter becuase the GeoCoordiante class's constructor is called with a couple of parameters.
A paramaterless constructor is a constructor for a type that does not take any parameters.
Upvotes: 0