title: Get results within x miles Using GeoCoordinate with respository pattern
body: <p>I would like to get a list of all organisations within x miles of a location entered by the user. This is converted to a long/lat location.</p>
  
  <p>Organisations are stored in the database with long and lat.</p>
  
  <p>I am using MVC with entity framework and a Unit of Work with the respository pattern to access the dataset.</p>
  
  <p>Here is my EntityRepository:</p>
  
  <pre><code>        public IQueryable&lt;T&gt; All
      {
          get
          {
              return dbSet;
          }
      }
  
      public IQueryable&lt;T&gt; AllIncluding(params System.Linq.Expressions.Expression&lt;Func&lt;T, object&gt;&gt;[] includeProperties)
      {
          IQueryable&lt;T&gt; query = dbSet;
          foreach (var includeProperty in includeProperties)
          {
              query = query.Include(includeProperty);
          }
  
          return query;
      }
  
      public IEnumerable&lt;T&gt; Where(System.Linq.Expressions.Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return dbSet.Where(predicate).AsEnumerable();
      }
  </code></pre>
  
  <p>To query the data in my datacontext I use a service class for each entity, a UOW is injected into each service. The service call for organisations is:</p>
  
  <pre><code> public class OrgService :IOrgService
  {
      private IUnitOfWork _UoW;
  
      public OrgService(IUnitOfWork UoW)
      {
          _UoW = UoW;
      }
  
      public Organisation GetOrgByID(int OrgID)
      {
  
          return _UoW.OrganisationRepo.Find(OrgID);
      }
  
      public IList&lt;Organisation&gt; GetAllOrgs()
      {
          return _UoW.OrganisationRepo.All.ToList();  
      }
  
      public IList&lt;Organisation&gt; GetOrgsByLocation(double lat, double lng, int range)
      {
  
        /// I need to return a list of all organisations within X miles
  
      }
  
   }
  </code></pre>
  
  <p>All other queries are working as the they should however I am no trying to write the method GetOrgsByLocation(). This is the query I think I need to get my results:</p>
  
  <pre><code> var userLocation = new GeoCoordinate(lat, lng);
   var result = _UoW.OrganisationRepo.Where(x =&gt; new GeoCoordinate(x.Latitude, x.Longitude))
                                .Where(x =&gt; x.GetDistanceTo(userLocation) &lt; radius).ToList();
  </code></pre>
  
  <p>When I try to run this query I get:</p>
  
  <p><strong><em>"cannot implicitly convert type system.device.location.geoCoordinate to bool"</em></strong></p>
  
  <p>Can anyone help?</p>
  
  <p>** Update - Working solution **</p>
  
  <pre><code> var userLocation = new GeoCoordinate(lat, lng);
          var nearbyOrganizations = _UoW.OrganisationRepo.All.ToList()
           .Select(x =&gt; new
           { //use an anonymous type or any type you want
               Org = x,
               Distance = new GeoCoordinate(x.Latitude, x.Longitude).GetDistanceTo(userLocation)
           }) 
           .Where(x =&gt; x.Distance &lt; 50000)
           .ToList();
  
          foreach (var organisation in nearbyOrganizations)
          {
              Console.WriteLine("{0} ({1:n0} meters)", organisation.Org, organisation.Distance);
          }
  </code></pre>
  
  <p>Thanks to the help below for this solution, though it seems all objects have to be queried in orfer for this to work, it seems that query would be better suited to run on the database, I'll have to look into this more.</p>