Bojbaj Bojbaj - 3 months ago 8
C# Question

report on date column with gap

there is my columns

Id | date | location


and some of rows :

1 | 2016/09/08 14:02:08 | NY
1 | 2016/09/08 14:03:08 | CA
1 | 2016/09/08 14:05:08 | SI
1 | 2016/09/08 14:07:05 | NY
1 | 2016/09/08 14:07:09 | NY
1 | 2016/09/08 14:07:22 | NY
1 | 2016/09/08 14:09:08 | SI
1 | 2016/09/08 14:23:08 | NY
1 | 2016/09/08 14:25:08 | LA


i want to know that :

"where was user at least every 5 minutes"


so my result is :

1 | 2016/09/08 14:02:08 | NY
1 | 2016/09/08 14:07:09 | NY
1 | 2016/09/08 14:23:08 | NY


i don't want any more rows between my results.

so, how can i do that ?
any solution such as "SQL Query", "LinQ Expression", "C# code", ... will be helpful.

thank you :)

Answer

Considering the class:

public class LocationInformation
{
    public int ID { get; set; }
    public DateTime Date { get; set; }
    public string Location { get; set; }

    public override string ToString()
    {
        return String.Format("Guy with ID : ({0}), was near {1} at {2}", ID, Location, Date);
    }
}

And the function:

public static void Locate(List<LocationInformation> myInformations, int id)
{
    DateTime lastCheck = DateTime.MinValue;
    foreach (LocationInformation locationInformation in myInformations.Where(i => i.ID == id).OrderBy(i => i.Date))
    {
        // Less than 5 min check
        if (locationInformation.Date < lastCheck.AddMinutes(5))
        {
            continue;
        }
        lastCheck = locationInformation.Date;
        Console.Out.WriteLine(locationInformation);
    }
}

This will do the work.