2016-09-08 7 views
0

gibt es meine SpaltenBericht über Datumsspalte mit Spalt

Id | date | location 

und einige der Zeilen:

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 

ich will, das wissen:

"where was user at least every 5 minutes" 

so mein Ergebnis ist:

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

ich möchte keine Zeilen mehr zwischen meinen Ergebnissen haben.

also, wie kann ich das tun? jede Lösung wie "SQL-Abfrage", "LinQ-Ausdruck", "C# -Code", ... wird hilfreich sein.

danke

+1

Versuchen Haben Sie versucht, irgendetwas? – sr28

+0

Ich versuchte etwas wie "Thomas Ayoub" Antwort. Filter Zeilen nach dem Lesen aus der Datenbank. – Bojbaj

Antwort

2

In Anbetracht der Klasse :):

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

und die Funktion:

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

Dies wird die Arbeit machen.

+0

danke. und ja, das ist eine gute Lösung. aber wenn meine Zeilen in jeder Sekunde eingefügt werden, kann mein Datenbankergebnis sehr groß sein und wenn meine Benutzer erwachsen werden, wird das Abfrageergebnis sehr lange dauern. Ich bin auf der Suche nach einer Lösung, die Datenbankzeilen vor dem Abrufen filtern kann. aber Plan B ist deine Lösung;) – Bojbaj

+0

* mein Datenbankergebnis kann sehr groß sein * => wie groß? @Bojbaj –

+0

etwa 3000x15x [Anzahl der Benutzer] Standort Zeilen pro Tag und über 240.000 Benutzer – Bojbaj

0

dieses

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication1 
{ 
    public class Program 
    { 

     public static void Main() 
     { 
      string[] inputs = { 
       "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" 
          }; 

      DataTable dt = new DataTable(); 
      dt.Columns.Add("Id", typeof(int)); 
      dt.Columns.Add("date", typeof(DateTime)); 
      dt.Columns.Add("location", typeof(string)); 

      foreach (string input in inputs) 
      { 
       string[] splitRow = input.Split(new char[] {'|'}); 
       dt.Rows.Add(new object[] { 
        int.Parse(splitRow[0]), 
        DateTime.Parse(splitRow[1]), 
        splitRow[2].Trim() 
       }); 
      } 
      var groups = dt.AsEnumerable() 
       .GroupBy(x => x.Field<string>("location")) 
       .Select(x => new { 
        id = x.First().Field<int>("Id"), 
        location = x.First().Field<string>("location"), 
        times = x.Select(y => y.Field<DateTime>("date")).ToList() 
       }).ToList(); 

      foreach (var group in groups) 
      { 
       DateTime lastTime = new DateTime(); 
       foreach (var date in group.times) 
       { 
        if (lastTime.AddMinutes(5) <= date) 
        { 
         Console.WriteLine(string.Join(",", new string[] { group.id.ToString(), group.location, date.ToString()})); 
         lastTime = date; 
        } 

       } 
      } 
      Console.ReadLine(); 
     } 

    } 
} 
+0

danke, gleiche Idee :) – Bojbaj

Verwandte Themen