找到距离最近的5000米前的记录

时间:2019-06-08 07:06:35

标签: c# asp.net-mvc linq

我有一台自行车电脑,每秒记录一次我走了多远

数据的简化版本如下:

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},
            };

我想找到最接近5000米的记录

该应用程序是使用Visual Studio 2019使用.net CORE 2.0构建的。

3 个答案:

答案 0 :(得分:2)

请尝试这个。

double closest = trackPointList.Select(t => t.DistanceMeters).Aggregate((x,y) => Math.Abs(x - 5000) < Math.Abs(y - 5000) ? x : y);

预期结果

5003.4101562500000000

答案 1 :(得分:0)

取大于等于5000m的所有距离的最小值。

var result = trackPointList.Where(e => e.DistanceMeters >= 5000).Min(e => e.DistanceMeters);

答案 2 :(得分:-1)

就这么简单

var result = trackPointList
    .OrderBy( e => System.Math.Abs( e.DistanceMeters - 5000 ) )
    .First();

.net fiddle sample