views:

43

answers:

1

Suggestion either in C# or VB.NET are welcome.

Table relationship:

  • Student 1:N TimeSheet (FK StudentId)
  • TimeSheet 1:N TimeRecord (FK TimeSheetId)

    Dim query = From s In db.Students _
                Let pair = (From ts In db.TimeSheets _
                Join tr In db.TimeRecords On tr.TimeSheetId Equals ts.TimeSheetId _
                Where ts.IsArchive = False And ts.IsCompleted = False _
                Group By key = New With {ts.TimeSheetId, ts.StudentId} Into TotalHour = Sum(tr.BonusHour)) _
                From part In pair _
                Where part.key.StudentId = s.StudentId _
                Select New With {.StudentId = s.StudentId, .AssignedId = s.AssignedId,.TotalTime = part.TotalHour}
    

Here's the result of the query:

  • 734 -- 159 : 9 hrs 35 mm 28 sec
  • 2655 -- 160 : 93 hrs 33 mm 50 sec
  • 1566 -- 161 : 37 hrs 23 mm 53 sec
  • 3114 -- 162 : 25 hrs 0 mm 21 sec

Wanted result of query:

  • 733 -- 158 : 0 hr 0mm 0 sec
  • 734 -- 159 : 9 hrs 35 mm 28 sec
  • 736 -- 169 : 0 hrs 0mm 0sec
  • 2655 -- 160 : 93 hrs 33 mm 50 sec
  • 1566 -- 161 : 37 hrs 23 mm 53 sec
  • 3114 -- 162 : 25 hrs 0 mm 21 sec
  • 2165 -- 189 : 0 hr 0 mm 21 sec

There are some TimeSheet that have no TimeRecord, which I need to select as well. How can I select all of them to make selection like above wanted result? I'm thinking of how I can include some condtion checking in the query to see if this TimeSheet has no TimeRecord then no need to Sum(tr.BonusHour) just assign TotalHour to zero. I don't know it's right way to go.

Any sugestion is welcome. Thanks

A: 

You can try doing something like this with the Sum (C#):

Sum(tr.BonusHour ?? 0)

which would be the same as

Sum(tr.BonusHour != null ? tr.BonusHour : 0)

I am not sure what type your BonusHour has, so you would use zero-correspondent object of this type instead of the 0 in the sample.

Yakimych