查找最大值和最小值的DateTime Linq中组 [英] Find max and min DateTime in Linq group

查看:1128
本文介绍了查找最大值和最小值的DateTime Linq中组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个CSV导入试图找到最大和最小的DateTime 秒。

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

我有

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



随着思想, g.Min 栽培大豆会给我的最低和最高的DateTime 每个时间表(按索引分组)

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

然而,这并不工作...请告诉我一个组内发现的DateTime对象的最高值和最低值的最佳方式?

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

这篇关于查找最大值和最小值的DateTime Linq中组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆