报告

2016-09-08 40 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