如何在LINQ to SQL中使用第一次联接的结果

时间:2019-07-19 13:05:33

标签: c# sql linq linq-to-sql

我有2条记录的列表表。列表A和列表B。列表A在任务表中有4个任务,列表B在任务表中有2个任务。列表A的任务1在TimeLog表中有两个时间日志条目。不使用外键。

现在考虑以下代码。

var objs = from project_task_list in modelRepository.Get()
                       join project_task in projectTaskRepository.Get() on project_task_list.ListId equals project_task.ListId into project_task_result
                       join project_task_timelog in projectTaskTimeLogRepository.Get() on project_task.TaskId equals project_task_timelog.TaskId into project_task_timelog_result
                       where project_task_list.ProjectId == project_id
                       select new ProjectTaskListModel
                       {
                           ListId = project_task_list.ListId,
                           ProjectId = project_task_list.ProjectId ?? 0,
                           ListName = project_task_list.ListName,
                           ListStartDate = project_task_list.ListStartDate,
                           ListEndDate = project_task_list.ListEndDate,
                           ListStatusId = project_task_list.ListStatusId ?? 0,
                           ListSortOrder = project_task_list.ListSortOrder ?? 0,

                           ListTasksEstimatedHours = project_task_result.Sum(x => x.TaskEstimatedHours) ?? 0,
                           ListTasksTimeLogTotalMinutes = project_task_timelog_result.Sum(x => x.LogMinutes) ?? 0
                       };

我想在另一个联接中使用一个联接的结果,但是出现以下语法错误。

enter image description here

我也尝试了以下代码。

var objs = from project_task_list in modelRepository.Get()
                       join project_task in projectTaskRepository.Get() on project_task_list.ListId equals project_task.ListId into project_task_result
                       from project_task in project_task_result.DefaultIfEmpty()
                       join project_task_timelog in projectTaskTimeLogRepository.Get() on project_task.TaskId equals project_task_timelog.TaskId into project_task_timelog_result
                       where project_task_list.ProjectId == project_id
                       select new ProjectTaskListModel
                       {
                           ListId = project_task_list.ListId,
                           ProjectId = project_task_list.ProjectId ?? 0,
                           ListName = project_task_list.ListName,
                           ListStartDate = project_task_list.ListStartDate,
                           ListEndDate = project_task_list.ListEndDate,
                           ListStatusId = project_task_list.ListStatusId ?? 0,
                           ListSortOrder = project_task_list.ListSortOrder ?? 0,

                           ListTasksEstimatedHours = project_task_result.Sum(x => x.TaskEstimatedHours) ?? 0,
                           ListTasksTimeLogTotalMinutes = project_task_timelog_result.Sum(x => x.LogMinutes) ?? 0
                       };

语法错误消失了,但是问题是它返回了6条记录,而我在列表表中只有2条记录。我究竟做错了什么?预先感谢。

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了自己。我需要的只是LINQ中的GROUP BY子句。这是最终查询。

var objs = from project_task_list in modelRepository.Get()
                       join project_task in projectTaskRepository.Get() on project_task_list.ListId equals project_task.ListId into project_task_result
                       from project_task in project_task_result.DefaultIfEmpty()
                       join project_task_timelog in projectTaskTimeLogRepository.Get() on project_task.TaskId equals project_task_timelog.TaskId into project_task_timelog_result
                       from project_task_timelog in project_task_timelog_result.DefaultIfEmpty()
                       where project_task_list.ProjectId == project_id

                       group new { project_task_list, project_task, project_task_timelog } by new
                       {
                           project_task_list.ListId,
                           project_task_list.ProjectId,
                           project_task_list.ListName,
                           project_task_list.ListStartDate,
                           project_task_list.ListEndDate,
                           project_task_list.ListStatusId,
                           project_task_list.ListSortOrder
                       } into group_result

                       select new ProjectTaskListModel
                       {
                           ListId = group_result.Key.ListId,
                           ProjectId = group_result.Key.ProjectId ?? 0,
                           ListName = group_result.Key.ListName,
                           ListStartDate = group_result.Key.ListStartDate,
                           ListEndDate = group_result.Key.ListEndDate,
                           ListStatusId = group_result.Key.ListStatusId ?? 0,
                           ListSortOrder = group_result.Key.ListSortOrder ?? 0,

                           ListTasksEstimatedHours = group_result.Sum(x => x.project_task.TaskEstimatedHours) ?? 0,
                           ListTasksTimeLogTotalMinutes = group_result.Sum(x => x.project_task_timelog.LogMinutes) ?? 0
                       };