How to write a LINQ query to fetch only the boundary records for a condition. For example, Consider the below database table which represents tracking data received from a vehicle:
I wish to fetch only record 47890 and 47880. Which will give the start time and end time when vehicle was stopped.
Right now, in my query i fetch all the records and then take the first and the last. Also, the query need to be generic, I may have multiple stops for a vehicle. For Example:
Stop1 : 11:00 AM to 1:00 PM
Stop2 : 3:00 PM to 3:30 PM
and so on.
Here is the code I have written so far:
var sData = db.Vehicles
.Where(v => v.VehicleId == vehicleId)
.SelectMany(v => v.GsmDeviceLogs)
.Where(gs => gs.DateTimeOfLog > startDate && gs.DateTimeOfLog < endDate && gs.Speed < zeroSpeed && !gs.IgnitionOn)
.Select(v => new
{
DateTimeOfLog = v.DateTimeOfLog,
Location = v.Location
}).OrderBy(gs => gs.DateTimeOfLog).ToList();