简体   繁体   中英

Query to extract data based upon time stamp conditions

I'm building ac# application where it has to read the data in the following format.


   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

The time stamp is in the format

yyyy-mm-dd hh:mm:ss

The data is logged for every 15 seconds. Hence the difference between time stamps of any consecutive two rows should be 15 seconds.

For Example: Let's say I want to read IN column 3 values satisfying the following timestamp condition.

  1. Timestamp Difference (TimeDiff)
  2. Upper-limit TimeStamp (TimeUpper)
  3. Lower limit TimeStamp (TimeLower)

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

and the output should look like this

Column3
 89

The entire idea is to extract certain time window (between two-time stamps) of data points from the database where I can adjust the values I readIN using the Timediff as my control parameter.

I know I can use CTE in SQL server for this logic but I'm lost with C# LINQ to SQL.

How do I write the logic in C#? using LINQ

Any suggestions, ideas would be really helpful

Thank you in advance.

        string delimiter = ",";

        string sTimeUpper = "2017-04-25 14:16:30.000";
        string sTimeLower = "2017-04-25 14:15:30.000";

        DateTime TimeUpper = DateTime.Parse(sTimeUpper);
        DateTime TimeLower = DateTime.Parse(sTimeLower);

        Console.WriteLine($"TimeLower = {TimeLower}");
        Console.WriteLine($"TimeUpper = {TimeUpper}");

        List<string> res = (File.ReadAllLines(@"TimeStamp.txt")
            .Skip(1)
            .Select(line => line.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            .Where(fields => DateTime.Parse(fields[3]) > TimeLower && DateTime.Parse(fields[3]) < TimeUpper)
            .Select(fields => string.Join(",", fields))
            .ToList<string>());

        Console.WriteLine("Requested readings:");

        foreach (string item in res)
        {
            Console.WriteLine(item);
        }

The solution is depends on your need. mine is something like this if the timestamp column is already in DateTime format, then you can use

int secondDiff = 30;
DateTime timeUpper = DateTime.Parse("2017-04-25 14:16:30.000");
DateTime timeLower = DateTime.Parse("2017-04-25 14:15:30.000");

List<string> results = yourContext.TableName.AsEnumerable()
    .Where(t => t.TimeStamp.AddSeconds(secondDiff) <= timeUpper && 
        t.TimeStamp.AddSeconds(-secondDiff) >= timeLower);
    .Select(t => t.Column3).ToList();
// Output is { 89 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM