我有一台自行车电脑,每秒记录一次我走了多远
数据的简化版本如下:
public class TrackPoint
{
public virtual Guid Id { get; set; } = Guid.NewGuid();
public virtual DateTime Time { get; set; }
public virtual double DistanceMeters { get; set; }
}
var trackPointList = new List<TrackPoint>
{
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:48"), DistanceMeters = 3.8099999427795410},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:49"), DistanceMeters = 7.7600002288818359},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:50"), DistanceMeters = 12.3299999237060547},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:51"), DistanceMeters = 18.0000000000000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:52"), DistanceMeters = 24.8999996185302734},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:53"), DistanceMeters = 32.1599998474121094},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:54"), DistanceMeters = 40.7200012207031250},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:55"), DistanceMeters = 49.7599983215332031},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:57"), DistanceMeters = 68.6100006103515625},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:58"), DistanceMeters = 79.3199996948242188},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:18:00"), DistanceMeters = 100.1900024414062500},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:18:02"), DistanceMeters = 122.7099990844726563},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:18:03"), DistanceMeters = 134.1900024414062500},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:18:04"), DistanceMeters = 145.9199981689453125},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:18:05"), DistanceMeters = 158.4700012207031250},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:04"), DistanceMeters = 5003.4101562500000000}, //5000 meters driven
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:05"), DistanceMeters = 5018.7797851562500000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:06"), DistanceMeters = 5034.0498046875000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:07"), DistanceMeters = 5048.8901367187500000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:08"), DistanceMeters = 5063.8798828125000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:09"), DistanceMeters = 5079.0200195312500000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:13"), DistanceMeters = 5141.0600585937500000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:17"), DistanceMeters = 5201.7500000000000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:21"), DistanceMeters = 5261.8798828125000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:23"), DistanceMeters = 5290.2900390625000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:28"), DistanceMeters = 5363.7099609375000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:33"), DistanceMeters = 5435.9101562500000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:34"), DistanceMeters = 5450.8901367187500000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:35"), DistanceMeters = 5465.4199218750000000},
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:36"), DistanceMeters = 5480.5400390625000000},
};
对于行驶5公里后的每条记录,我想计算最后5公里的时间。
我的问题是,对于5000米之后的每条记录,要找到最接近5000米之前的记录。我已经看过MoreLinq的MinBy
扩展名,但是我不确定这是前进的方向
答案 0 :(得分:1)
让事情变得简单,您可以跟踪找到的最接近标记的最后一点:
var trackPointList = new List<TrackPoint>
{
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:17:48"), DistanceMeters = 3.8099999427795410},
// ...
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:24:04"), DistanceMeters = 5003.4101562500000000}, //5000 meters driven
// ...
new TrackPoint {Time = DateTime.Parse("2018-08-10 07:30:04"), DistanceMeters = 10003.4101562500000000}, //10000 meters driven
};
// determine how many marks you have according to the highest value recorded
int pointsCount = (int)trackPointList[trackPointList.Count - 1].DistanceMeters % 5000;
var lastPoint = trackPointList[0];
for (int i = 1; i < pointsCount; i++)
{
double point = i * 5000;
// find the closest to the current mark
var currentPoint = trackPointList.Where(x => x.DistanceMeters > point).First();
Console.WriteLine("At {0}, you have driven {1}, {2} for the past 5KM", currentPoint.Time, currentPoint.DistanceMeters, currentPoint.Time - lastPoint.Time);
lastPoint = currentPoint;
}
打印:
At 10/08/2018 07:24:04, you have driven 5003.41015625, 00:06:16 for the past 5KM
At 10/08/2018 07:30:04, you have driven 10003.41015625, 00:06:00 for the past 5KM
答案 1 :(得分:1)
通过将此方法添加到Trackpoint类中,您可以根据用户定义的距离获取前一个方法(如果未找到,则返回null)
public class TrackPoint
{
public virtual Guid Id { get; set; } = Guid.NewGuid();
public virtual DateTime Time { get; set; }
public virtual double DistanceMeters { get; set; }
public TrackPoint GetPreviousByDistance(double Distance, List<TrackPoint> TrackPointsPoints)
{
TrackPoint prevTrackpoint = null;
double prevDistance = this.DistanceMeters - Distance;
if (prevDistance > 0)
{
prevTrackpoint = TrackPointsPoints.Where(tp => tp.DistanceMeters < prevDistance).OrderByDescending(tp => tp.DistanceMeters).FirstOrDefault();
}
return prevTrackpoint;
}
}
找到正确的跟踪点后,您可以进行任何计算来计算时间和距离,并根据需要调整为5000m时间。
用法:
TrackPoint ThisTrackPoint = trackPointList[22]; //Or however you want to define it
TrackPoint PreviosTrackPoint = ThisTrackPoint.GetPreviousByDistance(5000, trackPointList); // Can change distance as needed
答案 2 :(得分:0)
using System.Linq;
...
Dictionary<TrackPoint, TimeSpan> TimeSpans = new Dictionary<TrackPoint, TimeSpan>();
foreach (var trackpoint in trackPointList.Where(x => x.DistanceMeters >= 5000))
{
double distance = trackpoint.DistanceMeters;
foreach (var trackpoint2 in trackPointList.Where(x => x.DistanceMeters < distance).Reverse())
{
if (trackpoint2.DistanceMeters <= distance - 5000)
{
TimeSpans.Add(trackpoint, trackpoint.Time - trackpoint2.Time);
break;
}
}
}
这对我有用。 我正在遍历距离> 5000的所有跟踪点。然后回去看看5000米之前的时间。