を使用Enumerable.GroupBy
してグループ化できますdate
。Dictionary<DateTime,int>
次に、キーが日付で、値がその日付の合計時間である を作成できます。
Dictionary<DateTime,int> dateGroups = table.AsEnumerable()
.GroupBy(r => r.Field<DateTime>("Date").Date)
.Select(g => new{
Date = g.Key,
TotalHours = g.Sum(r =>
DateTime.ParseExact(r.Field<string>("SwipeTime"),
"htt", CultureInfo.InvariantCulture).Hour)
}).ToDictionary(x => x.Date, x => x.TotalHours);
編集:それで、それはTotalHours
一日中だったので、今は望ましい最大最小計算です。また、目的のタイムスパン形式を「11:41 AM」に変更しました。次にDateTime.Parse(str).TimeOfDay
、タイムスパンを取得するために使用します。
Dictionary<DateTime, int> dateGroups = table.AsEnumerable()
.GroupBy(r => r.Field<DateTime>("Date").Date)
.Select(g => new
{
Date = g.Key,
TotalHours =
(g.Max(r => DateTime.Parse(r.Field<string>("SwipeTime")).TimeOfDay)
- g.Min(r => DateTime.Parse(r.Field<string>("SwipeTime")).TimeOfDay)).Hours
}).ToDictionary(x => x.Date, x => x.TotalHours);