如何在 LINQ to SQL 中将第一次加入的结果用于第二次加入
How to use result of first join in second join in LINQ to SQL
我的列表 table 有 2 条记录。列表 A 和列表 B。列表 A 在任务 table 中有 4 个任务,列表 B 在任务 table 中有 2 个任务。列表 A 的任务 1 在 TimeLog table.No 使用了外键中有两个时间日志条目。
现在考虑以下代码。
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
};
我想在另一个连接中使用一个连接的结果,但出现以下语法错误。
我也试过下面的代码。
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 条记录,而我在列表 table 中只有 2 条记录。我究竟做错了什么?提前致谢。
我自己弄明白了。我只需要 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
};
我的列表 table 有 2 条记录。列表 A 和列表 B。列表 A 在任务 table 中有 4 个任务,列表 B 在任务 table 中有 2 个任务。列表 A 的任务 1 在 TimeLog table.No 使用了外键中有两个时间日志条目。
现在考虑以下代码。
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
};
我想在另一个连接中使用一个连接的结果,但出现以下语法错误。
我也试过下面的代码。
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 条记录,而我在列表 table 中只有 2 条记录。我究竟做错了什么?提前致谢。
我自己弄明白了。我只需要 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
};