0

I am creating an timesheet app, I would like to display following:

For each employee into a grid:

Id, Name, TaskName, SunHours, MondayHours... and so on FridayHours

Employee_Table
empid
name

Timesheet
id
empid
taskid
sat
sun
mon
tues
wed
thurs
fri
sat

Task
taskid
taskname

Timesheet table

I am struggling to create the object from a linq query as I cannot access the timesheet specific data i.e sun, mon, tue values. Here is what I have done:

   var result = (from e in db.Employees
                          join t in db.TimeSheets on e.Id equals t.EmployeeId
                          group e by t.TaskId into g
                          orderby g.Key
                          select new
                          {
                              empid = g.Key,
                              name = g.Select(e => e.Name),
                              sat = g.Select(s => s.Saturday), // cannot access this info
                              sun = g.Select(s => s.Sunday),
                              mon = g.Select(s => s.Monday),
                              tue = g.Select(s => s.Tuesday),
                              wed = g.Select(s => s.Wednesday),
                              thurs = g.Select(s => s.Thursday),
                              fri = g.Select(s => s.Friday),
                          });

Update:

So, I have ended up with this - it seems to represent what I am after.

var res = (from e in db.Employees
               join t in db.TimeSheets on e.Id equals t.EmployeeId

               group t by new { t.EmployeeId } into g
               select new
               {
                   id = g.Key.EmployeeId,
                   data = g
                   });
Harry
  • 3,031
  • 7
  • 42
  • 67

1 Answers1

1

Based on this answer, grouping by 'e' only makes the 't' objects out of scope.

You can join to the employee table after grouping the Timesheet objects. Looks like you may also need .Sum(s => s.mon) instead of .Select(s => s.mon)

Edit: .Sum() not .Max()

Community
  • 1
  • 1
cjkeilig
  • 101
  • 7