你可以用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
,現在所需的最大-min計算。您也已將您所需的時間段格式更改爲「上午11:41」。然後我會用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);
SwipeTime集合是一個字符串? –
是的,它是一個字符串.. – sajad
沒有字段叫swipetype至於它是否刷卡或刷卡? – naveen