在Linq组中查找最大和最小日期时间(Find max and min DateTime in Linq group)

编程入门 行业动态 更新时间:2024-10-27 00:28:22
在Linq组中查找最大和最小日期时间(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 };

认为g.Min和g.Max会给我每个时间表的最低和最高DateTime (按索引分组)

然而,这不起作用...什么是在组内找到DateTimes的最高和最低值的最佳方法?

I'm trying to find the max and min DateTimes from a CSV import.

I have this to import the data from the 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 };

Which works fine. I then want to group the data and show the Start time and finish time which is the min / max of the respective fields.

So far I have this:

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 };

With the thinking that g.Min and g.Max would give me the lowest and highest DateTime for each timesheet (grouped by index)

This doesn't work however... Whats the best way of finding the highest and lowest value of DateTimes within a group?

最满意答案

试试这个

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), });

Try using this

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), });

更多推荐

本文发布于:2023-08-04 07:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大和   组中   最小   日期   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!