5

我正在尝试DateTime从 CSV 导入中找到 max 和 min 。

我有这个从 temp 导入数据DataTable

var tsHead = from h in dt.AsEnumerable()
         select new
                    {
                        Index = h.Field<string>("INDEX"),
                        TimeSheetCategory = h.Field<string>("FN"),
                        Date = DdateConvert(h.Field<string>("Date")),
                        EmployeeNo = h.Field<string>("EMPLOYEE"),
                        Factory = h.Field<string>("FACTORY"),
                        StartTime = DdateConvert(h.Field<string>("START_TIME")), //min
                        FinishTime = DdateConvert(h.Field<string>("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 值的最佳方法是什么?

4

1 回答 1

10

尝试使用这个

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),
     });
于 2013-03-12T17:10:14.440 回答