Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取表中每个键的总时间跨度_C#_Sql Server_Linq - Fatal编程技术网

C# 获取表中每个键的总时间跨度

C# 获取表中每个键的总时间跨度,c#,sql-server,linq,C#,Sql Server,Linq,我有一个包含以下数据的数据表: LINE | SIGN | DATE ----------------------------------------- 1 | 1 | 2015-03-02 11:23:25 1 | 1 | 2015-03-02 18:24:03 1 | 1 | 2015-03-03 05:38:49 1 | 0 | 2015-03-03 08:47:02 1 | 1 | 2015-03-03 14:01:3

我有一个包含以下数据的数据表:

LINE | SIGN | DATE
-----------------------------------------
   1 |   1  |  2015-03-02 11:23:25
   1 |   1  |  2015-03-02 18:24:03
   1 |   1  |  2015-03-03 05:38:49
   1 |   0  |  2015-03-03 08:47:02
   1 |   1  |  2015-03-03 14:01:31
   1 |   1  |  2015-03-03 21:11:53
   1 |   1  |  2015-03-04 09:34:04
   1 |   0  |  2015-03-04 15:29:27
   1 |   0  |  2015-03-04 19:28:33
数据是按日期排序的,如您所见,我的要求是获取信号的总分钟数。比如多少分钟的信号是1,多少分钟的信号是0

我需要一个LINQ查询来获得结果

目前我在想的是,我可能需要做一个for循环,并添加分钟数。我不知道这是否是唯一的方法:

int noOfMinutesFor1 = 0;
for (int i = 1; i < signalData.Count; i++)
{
    if((signalData[i - 1].SIGN == signalData[i].SIGN == 1) || (signalData[i].SIGN == 1 && signalData[i - 1].SIGN == 0))
    {
        noOfMinutesFor1 += (signalData[i].SIGN - signalData[i - 1].SIGN).TotalMinutes;
    }

}
int noOfMinutesFor1=0;
对于(int i=1;i
这就是我要做的:

其思想是将每个信号的所有日期分组,然后,在按降序排序日期后,减去每一对,并保留偏移小时和分钟(以分钟为单位)

var groups=dt.AsEnumerable().GroupBy(r=>(int)r[“Signal”]);
foreach(组中的var组)
{
int-groupMinutes=0;
var datesDescending=group.OrderByDescending(g=>g[“Date”]);
对于(int i=0;ii+1)
{
var date2=(DateTime)datesDescending.ElementAt(i+1)[“Date”];
var dateOffset=date1.Subtract(date2);
groupMinutes+=dateOffset.Hours*60+dateOffset.Minutes;
}
其他的
groupMinutes+=date1.Hour*60+date1.Minute;
}
WriteLine(“信号:{0},总分钟数:{1}”,group.Key,groupMinutes);
}
}
输出:


我需要一个LINQ查询来获得结果。如果你需要什么,不要问我们,而是去做。我知道如何获得时间跨度,我想要上面数据中时间之间的差异。@GertArnold dude,至少让我朝着正确的方向走,我想高效地做到这一点,但我看到的唯一方法是遍历每一行,检查信号是否已更改,并将分钟数添加到变量中。我需要知道是否可以通过其他方式完成。我没有关于您的要求的太多详细信息,但这是否是一个解决方案,可以总结信号为1时的时间,并提取信号为0时的总分钟数?谢谢Veverke,我会尝试让您知道
 public static void Example()
{
    DataTable dt = new DataTable();

    dt.Columns.Add("Signal", typeof(int));
    dt.Columns.Add("Date", typeof(DateTime));

    dt.Rows.Add(1, DateTime.ParseExact("2015-03-02 11:23:25", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(1, DateTime.ParseExact("2015-03-02 18:24:03", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(1, DateTime.ParseExact("2015-03-03 05:38:49", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(0, DateTime.ParseExact("2015-03-03 08:47:02", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(1, DateTime.ParseExact("2015-03-03 14:01:31", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(1, DateTime.ParseExact("2015-03-03 21:11:53", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(1, DateTime.ParseExact("2015-03-04 09:34:04", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(0, DateTime.ParseExact("2015-03-04 15:29:27", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    dt.Rows.Add(0, DateTime.ParseExact("2015-03-04 19:28:33", "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture));
    var groups = dt.AsEnumerable().GroupBy(r => (int)r["Signal"]);

    foreach (var group in groups)
    {
        int groupMinutes = 0;
        var datesDescending = group.OrderByDescending(g => g["Date"]);
        for (int i = 0; i < datesDescending.Count(); i += 2)
        {
            var date1 = (DateTime)datesDescending.ElementAt(i)["Date"];
            if (datesDescending.Count() > i + 1)
            {
                var date2 = (DateTime)datesDescending.ElementAt(i + 1)["Date"];
                var dateOffset = date1.Subtract(date2);
                groupMinutes += dateOffset.Hours * 60 + dateOffset.Minutes;
            }
            else
                groupMinutes += date1.Hour * 60 + date1.Minute;
        }

        Console.WriteLine("Signal: {0}, total minutes: {1}", group.Key, groupMinutes);
    }
}