在Linq组中查找最大和最小日期时间
我正在尝试从CSV导入中找到最大和最小DateTime
。
我有这个从临时DataTable
导入数据:
var tsHead = from h in dt.AsEnumerable() select new { Index = h.Field("INDEX"), TimeSheetCategory = h.Field("FN"), Date = DdateConvert(h.Field("Date")), EmployeeNo = h.Field("EMPLOYEE"), Factory = h.Field("FACTORY"), StartTime = DdateConvert(h.Field("START_TIME")), //min FinishTime = DdateConvert(h.Field("FINISH_TIME")), //max };
哪个工作正常。 然后我想对数据进行分组并显示开始时间和结束时间,即各个字段的最小值/最大值。
到目前为止我有这个:
var tsHeadg = from h in tsHead group h by h.Index into g //Pull out the unique indexes let f = g.FirstOrDefault() where f != null select new { f.Index, f.TimeSheetCategory, f.Date, f.EmployeeNo, f.Factory, g.Min(c => c).StartTime, //Min starttime should be timesheet start time g.Max(c => c).FinishTime, //Max finishtime should be timesheet finish time };
认为g.Min
和g.Max
会给我每个时间表的最低和最高DateTime
(按索引分组)
然而,这不起作用…什么是在组内找到DateTimes的最高和最低值的最佳方法?
试试这个
var tsHeadg = (from h in tsHead group h by h.Index into g //Pull out the unique indexes let f = g.FirstOrDefault() where f != null select new { f.Index, f.TimeSheetCategory, f.Date, f.EmployeeNo, f.Factory, MinDate = g.Min(c => c.StartTime), MaxDate = g.Max(c => c.FinishTime), });