简体   繁体   中英

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

I have db.

Here is screen

在此处输入图片说明

Here is model of 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

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 ). 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:

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

Update 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. Assuming that id is simply the id property in your model, it can be done as follows:

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. To tackle this issue simply check if the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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