So, i've got a problem with querying a specific data from my database. Lets say i have two contexts containing objects of type:
TrackPoint {
int Id;
double Latitude;
double Longitude;
int TrackId
{
Track {
int Id;
double MinLatitude;
double MaxLatitude;
double MinLongitude;
double MaxLongitude;
}
Every track has some amount of TrackPoints assigned. I want to query all trackpoints, whose tracks intersect with other track (areas constructed from min, max values overlap).
Im trying to achieve this within only one query, as the performance is important. I've managed to do this with the query below, but the execution time is not so good. Im sure there are better ways to this. I would appreciate any advice.
var similarTrackPoints = Context.TrackPoints.Include("Track").Where(
tp =>
Context.Tracks.Where(t => t.Id == tp.TrackId).FirstOrDefault().MinLongitude <= track.MaxLongitude &&
Context.Tracks.Where(t => t.Id == tp.TrackId).FirstOrDefault().MaxLongitude >= track.MinLongitude &&
Context.Tracks.Where(t => t.Id == tp.TrackId).FirstOrDefault().MinLatitude <= track.MaxLatitude &&
Context.Tracks.Where(t => t.Id == tp.TrackId).FirstOrDefault().MaxLatitude >= track.MinLatitude)
.ToList();
tp => { var x = Context.Tracks.Where(t => t.Id == tp.TrackId).FirstOrDefault(); return x.MinLongitude <= track.MaxLongitude && .... }Or maybe that is only legal ln LINQ for objects?Trackhas one or moreTrackPoints, then shouldn't the class contain a collection of them?