Find max and min DateTime in Linq group

I am trying to find the max and min DateTime from a CSV import.

I have this to import data from 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 great. Then I want to group the data and show the start time and end time, which is the minimum / maximum for the corresponding 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 }; 

Given that g.Min and g.Max will give me the lowest and highest DateTime for each schedule (grouped by index)

This does not work, however ... What is the best way to find the highest and lowest DateTimes in a group?

+4
source share
1 answer

Try using

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

Source: https://habr.com/ru/post/1468767/


All Articles