Wednesday, 15 January 2014

asp.net mvc 4 - How to set variables dynamically using Entity Framework 5 & Linq -



asp.net mvc 4 - How to set variables dynamically using Entity Framework 5 & Linq -

how set latitude & longitude of "sourcepoint" dynamically?

i can query database info need (the latitude & longitude) not sure how set sourcepoint dynamically using values.

this first shot @ c#, mvc 4, entity framework 5 & linq appreciate help on one. help in advance :)

public actionresult index(string location = "melbourne-vic-3000") { ////get latitude & longitude of current location //var latlong = // (from u in db.locations // u.url.equals(location) // select u).take(1); //set latitude & longitude of sourcepoint var sourcepoint = geoutils.createpoint(-37.815206, 144.963937); //***need latitude & lonitude database (see query above)*** //work out distance each business sourcepoint var model = x in db.businesses y in db.locations location == y.url select new businessdistance { businessname = x.businessname, address = x.address, distance = math.round((x.geolocation.distance(sourcepoint).value / 1000), 2) }; homecoming view(model.tolist()); } ---- working code ---- public actionresult index(string location = null) { var sourcepoint = geoutils.createpoint(-37.815206, 144.963937); //set default latitude & longitude melbourne //get latitude & longitude of current location database var latlong = (from u in db.locations u.url.equals(location) select u).firstordefault(); if (latlong != null) { //dynamically set latitude & longitude of sourcepoint using latitude & longitude got database sourcepoint = geoutils.createpoint((float)convert.todouble(latlong.latitude), (float)convert.todouble(latlong.longitude)); } //work out distance each business sourcepoint var model = x in db.businesses y in db.locations location == y.url select new businessdistance { locationsuburb = y.suburb, businessname = x.businessname, address = x.address, distance = math.round((x.geolocation.distance(sourcepoint).value / 1000), 2) }; if (model != null) { homecoming view(model.tolist()); } else { //display error page homecoming view(); //to do... } }

try this:

//get latitude & longitude of current location var latlong = (from u in db.locations u.url.equals(location) select u).firstordefault(); // declarre sourcepoint here if(latlong != null) { //set latitude & longitude of sourcepoint sourcepoint = geoutils.createpoint(latlong.latitude, latlong.longitude); }

linq asp.net-mvc-4 entity-framework-5

No comments:

Post a Comment