有我的专栏
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 Query"," LinQ Expression"," C#code",...将会有所帮助。
谢谢你:)答案 0 :(得分: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);
}
}
这将完成工作。
答案 1 :(得分: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();
}
}
}