2016-06-22 13 views
-1

に各キーの合計タイムスパンをゲット:あなたが見ることができるように、データを日付順に並べられ私は、次のデータを含むデータテーブルを持つテーブル

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; 
    } 

} 
+0

http://stackoverflow.com/questions/12521366/getting-time-span-between-two-times-in -c – pijemcolu

+0

*結果を得るにはLINQクエリが必要です。*何か必要な場合は、私たちに質問しないでください。 –

+0

私はTimeSpanを取得する方法を知っている、私は上記のデータの時間の違いが欲しい。 – progrAmmar

答えて

1

これは私がどうなるのかです:これが唯一の方法である場合、私は知りませんオフセット時間と分を分単位で保持します。

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)); 

enter image description here

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); 
    } 
} 

出力:

enter image description here

+0

ありがとうVeverke私は試してみましょう – progrAmmar

関連する問題