Convert GeoCoordinate


  • I have a list of stores
    var stores = mydbContext.AllStores();
    AllStores is of type Store.

    I need to get the closest store for the area the user has entered:
    var ClosestStores = stores.Select(x => new GeoCoordinate(x.Latitude, x.Longitude).AsEnumerable()
                                           .OrderBy(x => x.GetDistanceTo(coord))

    This brings back 10 stores.

    How could i convert ClosestStores to Store so i can access all my Store class properties?
    Wednesday, April 26, 2017 11:38 AM

All replies

  • Your select is returning a GeoCoordinate so you are loosing the store information. What you need to do is return an object that has both. If you only need this info within this one method then an anonymous type is fine.

    //I prefer LINQ syntax
    var closestStores = (from s in stores
                         orderby s.GetDistanceTo(...)
                         select new { Store = s, Coordinates = new GeoCoordinates(...) }).Take(10);
    foreach (var closest in closestStores)
       //Store = closest.Store
       //Coords = closest.Coordinates

    If you need to pass this data along to some other method then you'll need to create a regular type to contain this data or perhaps use a Tuple if you are only using the data within a single type.

    Michael Taylor

    Thursday, April 27, 2017 2:31 PM