Returning Entities + Extra Data with ADO.NET Data Services
Trying to figure out the best way to accomplish this. So I have a table of Stores with properties:
StoreID, Name, Latitude, Longitude, etc.
I'm using ADO.NET Data Services (Astoria) to create a web service that will show nearby stores given a radius and a coordinate. I made a Service Operation to take the params and return the results as an IQueryable. So far so good.
So right now I have something like:
return context.Stores.Where(...distance calculation...);
This works great, but I also want to return the distance of each store from the given radius. I have the calculation covered, but how should I include the distance along with the store data? Should I make a class like StoreSearchResult with properties for the Store and the Distance?
If I was just using SQL, I could write a single database query that would return all of the data for the Store, along with a column for the distance. Something like:
Select StoreID, Name, Latitude, Longitude, (calculation...) as Distance
from Store
where ...distance param...
So I'm looking for the best way to send back the original Store data + the distance calculation while still returning IQueryable. I'd pre开发者_如何学运维fer not to take an IEnumerable and use AsQueryable...I just feel like it should be possible to keep it "close" to the database query, and I'm probably just missing something.
It may be better to write a struct that holds store and distance then just add those to a list of matches
List<StoreSearchResult> matches = new List<StoreSearchResult>();
foreach (Store store in Stores)
{
int distance = ...distance calc...
if (distance < searchArea)
{
matches.Add(new StoreSearchResult(store, distance);
}
}
return matches;
Or, you could just add the current search distance as a variable on the Store class (which seems messy to me) to store distance, and then a method that will calculate it and update the distance value as well as return it.
int CalcDistance(lat, long)
{
... do some calc...
this.CurrentSearchDistance = results;
return results;
}
then...
return context.Stores.Where(store => store.CalcDistance(lat,long) < searchArea);
I posted this under the ADO.NET Data Services forum, and got some feedback from a MS member that was very useful.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/77769bd8-8955-4bad-997d-b13e1e691a21
I wound up just adding another physical column to my table and using a stored procedure, but it doesn't look like there's really a good solution to this issue at the moment, because using AsQueryable over the SP results gives me an EnumerableQuery, not ObjectQuery, so operators like $expand don't work.
Oh well, I guess I won't be using Astoria for much more than just prototyping at this stage, but it looks like it'll be improved in future versions.
精彩评论