Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set variables dynamically using Entity Framework 5 & Linq
    primarykey
    data
    text
    <p>How would I set the latitude &amp; longitude of the "sourcePoint" dynamically?</p> <p>I can query database to get the info I need (the latitude &amp; longitude) but I am not sure how to set the sourcePoint dynamically using those values.</p> <p>This is my first shot at C#, MVC 4, Entity Framework 5 &amp; Linq so I would appreciate some help on this one. Thanks for your help in advance :)</p> <pre><code> public ActionResult Index(string location = "melbourne-vic-3000") { ////get the latitude &amp; longitude of the current location //var latlong = // (from u in db.Locations // where u.Url.Equals(location) // select u).Take(1); //set the latitude &amp; longitude of the sourcePoint var sourcePoint = GeoUtils.CreatePoint(-37.815206, 144.963937); //***NEED TO GET THE LATITUDE &amp; LONITUDE FROM THE DATABASE (see query above)*** //work out the distance each business is from the sourcePoint var model = from x in db.Businesses from y in db.Locations where location == y.Url select new BusinessDistance { BusinessName = x.BusinessName, Address = x.Address, Distance = Math.Round((x.GeoLocation.Distance(sourcePoint).Value / 1000), 2) }; return View(model.ToList()); } </code></pre> <hr> <h2>---- working code ----</h2> <pre><code> public ActionResult Index(string location = null) { var sourcePoint = GeoUtils.CreatePoint(-37.815206, 144.963937); //Set the default latitude &amp; longitude to Melbourne //get the latitude &amp; longitude of the current location from the database var latlong = (from u in db.Locations where u.Url.Equals(location) select u).FirstOrDefault(); if (latlong != null) { //dynamically set the latitude &amp; longitude of the sourcePoint using the latitude &amp; longitude we got from the database sourcePoint = GeoUtils.CreatePoint((float)Convert.ToDouble(latlong.Latitude), (float)Convert.ToDouble(latlong.Longitude)); } //work out the distance each business is from the sourcePoint var model = from x in db.Businesses from y in db.Locations where 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) { return View(model.ToList()); } else { //display the error page return View(); //TO DO... } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload