如何使用C#中的LINQ查询将以前的记录列值与列表的当前记录列值进行比较

时间:2018-02-19 04:55:30

标签: c# linq

将先前的记录列值与c#linq

中的当前记录列值进行比较

我的linq查询如下,

var list = (from v in db.VehicleLocation
            where v.VehicleId == vehicleId
            select new VehicleLocationModel {
                        Id = v.Id,
                        Location = v.Location,
                        DateTimestamp = v.DateTimestamp,
                        DiffTimestamp = v.DateTimestamp - previoustimestamp
            }).OrderBy(x => x.DateTimestamp).ToList();

请帮帮我......

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情:

var list = (from v in db.VehicleLocation.Where(x.VehicleId == vehicleId)
            from v2 in db.VehicleLocation.Where(x.VehicleId == vehicleId)
            where v.DateTimestamp > v2.previoustimestamp
            group v2 by new { v.Id, v.Location, v.DateTimestamp } into sub
            select new VehicleLocationModel
            {
                Id = sub.Key.Id,
                Location = sub.Key.Location,
                DateTimestamp = sub.Key.DateTimestamp,
                DiffTimestamp = sub.Key.DateTimestamp - sub.Max(x => x.DateTimestamp)
            }).OrderBy(x => x.DateTimestamp).ToList();

答案 1 :(得分:0)

所以你有一系列的VehicleLocations,其中一些是来自带有vehicleId的车辆。每个VehicleLocation都有一个TimeStamp。

你想要与其他一些属性一起使用具有vehicleId的车辆的所有VehicleLocations,以及它们的TimeStamps和DiffTimeStamp的值,这是TimeStamp与你所说的之间的差异"之前的时间戳"

首先,您必须定义上一个时间戳。我想,你的意思是,如果你通过提升的时间戳订购一个特定车辆的所有VehicleLocations,那么"之前的时间戳"除了第一个VehicleLocation之外的任何一个是当前之前的VehicleLocation的时间戳。

使定义完成:第一个元素的上一个时间戳是元素本身的时间戳。这使得DiffTimeStamp成为当前时间戳和上一个时间戳之间的差异。序列中第一项的DiffTimeStamp是TimeSpan.Zero

我认为最快的方法是将带有vehicleId的车辆的所有VehicleLocations的有序序列(请求的属性)传输到本地内存,然后返回所请求的数据:

IEnumerable<VehicleLocationModel> FetchModelsById(int vehicleId)
{
    var vehicleLocations = db.VehicleLocations
        .Where(vehicleLocation => vehicleLocation.VehicleId == vehicleId)
        .Select(vehicleLocation => new VehicleLocationModel()
        {
            Id = vehicleLocation.Id,
            Location = vehicleLocation.Location,
            DateTimeStamp = vehicleLocation.DateTimestamp,
        })
        .OrderBy(vehicleLocation => vehicleLocation.TimeStamp);

注意:除了DiffTimeStamp之外的所有值都已填充。如果集合包含元素,我们只会返回返回的VehicleLocations。第一个元素的DiffTimeStamp将等于TimeSpan.Zero:

继续:

        // only yield return something if there are elements:
        if (vehicleLocations.Any())
        {
            // the first one will be different:
            var firstElement = vehicleLocations.First();
            firstElement.DiffTimeStamp = TimeSpan.Zero;
            yield return firstElement;

            // the rest of the elements:
            DateTime previousTimeStamp = firstElement.DateTimeStamp;
            foreach (VehicleLocation location in vehicleLocations.Skip(1))
            {
                 location.DiffTimeStamp = location.DateTimeStamp - previousTimeStamp;
                 yield return location;
                 previousTimeStamp = location.DateTimeStamp;
            }
        }
    }
}

这个解决方案的好处(除了它易于理解)是数据库必须做更少的工作,它必须将更少的字节传输到本地进程(最慢的部分),以及原始序列数据库端和本地端的结果序列只迭代一次。这是以您的本地进程必须进行DatetimeStamp和PreviousDateTimeStamp的减法为代价的。但这是每次迭代元素最多完成一次