2013-03-12 2 views
4

CSV 가져 오기에서 최대 및 최소 DateTime을 찾으려고합니다. 잘 작동Linq 그룹의 최대 및 최소 DateTime 찾기

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

을 :

나는이 임시 DataTable에서 데이터를 가져올 수 있습니다. 그런 다음 데이터를 그룹화하고 각 필드의 최소/최대 인 시작 시간과 종료 시간을 표시하려고합니다.

지금까지 나는이 있습니다 g.Ming.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 
           }; 

을 (인덱스별로 그룹화) 각 작업 표

이 그러나 작동하지 않습니다에 대한 DateTime 최저 및 최고 ... 그룹 내에서 DateTimes의 가장 높은 값과 가장 낮은 값을 찾는 가장 좋은 방법은 무엇입니까?

+0

주의를 사용해보십시오. 이 타입에 대한 자연 순서 부는 정의되어 있지 않으므로'.Min' 또는'.Max'를 찾을 의미가 없습니다. 아마도'g.Min (c => c.StartTime)'을 의미했을까요? – mellamokb

+0

@mellamokb 나는 p.s.w.g가 더 낮은 점을 지적하고 내 문제를 해결했습니다. –

답변

7

`g.Min (C => c)는 '첫 번째 쿼리에서 익명 형식의 전체 개체 인스턴스의 최소 찾을 의미이

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

감사합니다. p.sw.w.g, perfect –

관련 문제