查询以根据时间戳条件提取数据

我正在构建ac#应用程序,其中它必须读取以下格式的数据。


列1列2列3时间戳

   10 20 30 2017-04-25 14:15:00.000

   12 30 40 2017-04-25 14:15:15.000

   55 54 89 2017-04-25 14:15:30.000

   66 78 11 2017-04-25 14:15:45.000

   12 30 40 2017-04-25 14:16:00.000

   55 54 89 2017-04-25 14:16:15.000

   66 78 11 2017-04-25 14:16:30.000

时间戳记采用以下格式


yyyy-mm-dd hh:mm:ss


每15秒记录一次数据。因此,任何连续两行的时间戳之间的差应为15秒。


例如:假设我要读取满足以下时间戳条件的IN列3值。


时间戳差异(TimeDiff)

上限时间戳(TimeUpper)

下限时间戳(TimeLower)


TimeDiff> = 30秒TimeUpper = 2017-04-25 14:16:30.000 TimeLower = 2017-04-25 14:15:30.000


并且输出应如下所示


Column3

 89

整个想法是从数据库中提取数据点的特定时间窗口(两次时间戳之间),在该窗口中,我可以使用Timediff作为控制参数来调整readIN的值。


我知道我可以在SQL Server中使用CTE来实现这种逻辑,但是我对C#LINQ to SQL迷失了。


如何用C#编写逻辑?使用LINQ


任何建议,想法将非常有帮助


先感谢您。


潇潇雨雨
浏览 248回答 2
2回答

烙印99

&nbsp; &nbsp;string delimiter = ",";&nbsp; &nbsp; &nbsp; &nbsp; string sTimeUpper = "2017-04-25 14:16:30.000";&nbsp; &nbsp; &nbsp; &nbsp; string sTimeLower = "2017-04-25 14:15:30.000";&nbsp; &nbsp; &nbsp; &nbsp; DateTime TimeUpper = DateTime.Parse(sTimeUpper);&nbsp; &nbsp; &nbsp; &nbsp; DateTime TimeLower = DateTime.Parse(sTimeLower);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"TimeLower = {TimeLower}");&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"TimeUpper = {TimeUpper}");&nbsp; &nbsp; &nbsp; &nbsp; List<string> res = (File.ReadAllLines(@"TimeStamp.txt")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Skip(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(line => line.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(fields => DateTime.Parse(fields[3]) > TimeLower && DateTime.Parse(fields[3]) < TimeUpper)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(fields => string.Join(",", fields))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList<string>());&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Requested readings:");&nbsp; &nbsp; &nbsp; &nbsp; foreach (string item in res)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(item);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP