提问人:Gordon Copestake 提问时间:3/13/2013 最后编辑:D StanleyGordon Copestake 更新时间:3/13/2013 访问量:12238
在 Linq 组中查找最大和最小 DateTime
Find max and min DateTime in Linq group
问:
我正在尝试从 CSV 导入中找到最大值和最小值。DateTime
我有这个从临时导入数据: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
};
考虑到 and 会给我每个时间表的最低和最高(按索引分组)g.Min
g.Max
DateTime
然而,这不起作用......在组中查找 DateTimes 的最高值和最低值的最佳方法是什么?
答:
11赞
p.s.w.g
3/13/2013
#1
尝试使用它
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),
});
评论
g.Min(c => c)
.Min
.Max
g.Min(c => c.StartTime)