報告

2016-09-08 38 views
0

有我的專欄報告

Id | date | location 

和一些行:

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 

我想知道的是:

"where was user at least every 5 minutes" 

所以我的結果是:

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

我不希望我的結果之間有更多的行。

那麼,我該怎麼做?任何解決方案,如「SQL查詢」,「LinQ表達式」,「C#代碼」......都會有幫助。

謝謝:)

+1

你嘗試過什麼? – sr28

+0

我試過類似「托馬斯阿尤布」的答案。 從數據庫讀取後過濾行。 – Bojbaj

回答

2

考慮到類:

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

而且功能:

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

這將做的工作。

+0

謝謝。 是的,這是一個很好的解決方案。 但是如果我的行每秒插入一次,那麼我的數據庫結果可能非常大,如果我的用戶長大,查詢結果將花費很長時間。 我正在尋找一種解決方案,可以在首先提取它們之前過濾數據庫行。但計劃B是你的解決方案;) – Bojbaj

+0

*我的數據庫結果可能很大* =>有多大? @Bojbaj –

+0

約3000x15x [用戶數量]每天的位置行數 和約240,000個用戶 – Bojbaj

0

試試這個

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

謝謝你,同樣的想法:) – Bojbaj