c# - How to Get Closest Location in LINQ to Entities -
i trying closest location via 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, getting following error:
only parameterless constructors , initializers supported in linq entities.
i have tried googling still not sure how fix it. parameterless constructor?
you need try 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;
Comments
Post a Comment