Search code examples
c#.netlinqentity-frameworkgps

GPS coordinates with Entity framework and LINQ


So from what I read the Geography SQL type isn't supported in the entity framework yet.

I was wondering if anyone had a solution to a problem I'm having. I have a c# class that has some data access methods using the entity framework and LINQ. These methods are getting GPS coordinates passed into them. The database (and entity framework types) have longitude and latitude fields on them which are populated when the rows are created. This all works fine.

What I want to do is to make my LINQ queries pull back rows that are near (within a radius) the given GPS coordinates. I don't want to have to start using stored procs as this will be an architecture change.

Does anyone have any ideas on this? I'm struggling for ideas.


Solution

  • If you want to take advantage of your database Spatial index (which will allow fast spatial queries), you should store your positions as Geography in your table (you can also keep the current latitude/longitude columns if you want to access to this values).

    As Entity Framework doesn't support Geography types/functions for now, I'm afraid your only option is to write a stored procedure that internally calls STDistance (if you use SQL Server 2008 R2).

    Honestly, as the spatial functions actually depend on the database (STDistance is valid for SQL Server 2008 R2 only), it's not a bad idea to have stored procedure for your particular use case.