Encuentra max y min DateTime en el grupo Linq
Estoy tratando de encontrar el máximo y mínimoDateTime
s de una importación CSV.
Tengo esto para importar los datos de la 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
};
Que funciona bien. Luego quiero agrupar los datos y mostrar la hora de inicio y la hora de finalización, que es el mínimo / máximo de los campos respectivos.
Hasta ahora tengo esto:
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
};
Con el pensamiento de queg.Min
yg.Max
me daría lo mas bajo y lo mas altoDateTime
para cada hoja de tiempo (agrupada por índice)
Sin embargo, esto no funciona ... ¿Cuál es la mejor manera de encontrar el valor más alto y más bajo de DateTimes dentro de un grupo?