简体   繁体   English

获取具有特定数据的上一个元素 (ASP.NET MVC)

[英]Get previous element with specific data (ASP.NET MVC)

I have db.我有数据库。

Here is screen这是屏幕

在此处输入图片说明

Here is model of db.这是db的模型。

public partial class Logging
{
    public string Imei { get; set; }
    public DateTime CurDateTime { get; set; }
    public Nullable<System.DateTime> GPSDateTime2 { get; set; }
    public Nullable<decimal> Latitude2 { get; set; }
    public Nullable<decimal> Longitude2 { get; set; }
    public int Speed { get; set; }
    public Nullable<int> Datatype { get; set; }
    public int Id { get; set; }
}

I need to find difference in minutes between entry where datatype == 1 and previous element where datatype == 2我需要找到datatype == 1条目和datatype == 2前一个元素之间的分钟差异

on back-end I make this code to get all data from table在后端,我使用此代码从表中获取所有数据

using (var ctx = new GoogleMapTutorialEntities())
{
    var allitems = ctx.Loggings.AsEnumerable().Select(
            x => new Logging
            {
                Longitude2 = x.Longitude2,
                Latitude2 = x.Latitude2,
                CurDateTime = x.CurDateTime,
                Datatype = x.Datatype

            }).ToList(); 
}

How I can calculate difference?我如何计算差异?

You should use the Ticks property of the DateTime and pass it to a TimeSpan in order to get the time difference in minutes ( TotalMinutes ).您应该使用DateTimeTicks属性并将其传递给TimeSpan以获得以分钟为单位的时差 ( TotalMinutes )。 For example:例如:

for(int i = 1; i < yourQuery.Count; i++){
    var differenceTicks = yourQuery[i].CurDateTime.Ticks - yourQuery[i-1].CurDateTime.Ticks;
    var differenceInMinutes = new TimeSpan(differenceTicks).TotalMinutes;
    // You can assign this value to any property within your list or 
    // to another list
} 

Update:更新:

To filter the query with the DataType property, you should call the .Where(...) method and you can use it this way:要使用DataType属性过滤查询,您应该调用.Where(...)方法,您可以这样使用它:

 var filteredQuery = query.Where(x=> x.DataType == 1 || x.DataType == 2);

Update 2:更新 2:

The problem you described in your comment is more complex and to solve this problem you need to take advantage of the Record Id/Row Identifier in actual table.您在评论中描述的问题更复杂,要解决此问题,您需要利用实际表中的记录 ID/行标识符。 Assuming that id is simply the id property in your model, it can be done as follows:假设 id 只是模型中的id属性,可以按如下方式完成:

var filteredQuery = query.Where(x=> x.DataType == 1 || x.DataType == 2).OrderByDescending(x=> x.id);
for(int i = 1; i < filteredQuery.Count; i++){
    if(filteredQuery[i].DataType == 2 && filteredQuery[i-1].DataType == 1){
        // Then calculate the difference as described 
    }
}

You may need to tune the conditions in the loop according to what exactly you need.您可能需要根据您的需要调整循环中的条件。

Keep in mind that you may get a result in which DateType == 2 may not correspond to DataType == 1 as the data in between might have been already removed in the database.请记住,您可能会得到一个结果,其中DateType == 2可能与DataType == 1不对应,因为它们之间的数据可能已在数据库中删除。 To tackle this issue simply check if the current id is equal to previous id + 1 .要解决这个问题,只需检查current id is equal to previous id + 1 This only works if the id is not a GUID type (not even a sequential GUID) and of course not in Alphabetic form or generated by random functions.这仅适用于 id 不是 GUID 类型(甚至不是顺序 GUID)并且当然不是字母形式或由随机函数生成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM