LINQ查询语法按表达式麻烦

时间:2019-03-18 20:47:01

标签: c# linq

我正在创建一个时间表应用程序,我想显示以下内容:

对于每个进入网格的员工:

Id,名称,任务名称,SunHours,MondayHours ...等,在FridayHours

Employee_Table
empid
name

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

Task
taskid
taskname

Timesheet table

由于我无法访问时间表特定的数据(即sun,mon,tue值),因此无法根据linq查询创建对象。这是我所做的:

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

更新:

所以,我结束了这一点-似乎代表了我所追求的。

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

1 个答案:

答案 0 :(得分:1)

基于此answer,按“ e”分组只会使“ t”个对象超出范围。

可以在对时间表对象进行分组之后加入到雇员表。看来您可能还需要.Sum(s => s.mon)而不是.Select(s => s.mon)

编辑:.Sum()而不是.Max()