由于我是Linq的新手,因此无法将此SQL查询转换为Linq我粘贴了我已经尝试过的代码,它会提供空记录。
提前致谢。
这是我尝试过的Linq C#语句:
from ts in Db.Tasks
join prt in Db.ProjectTasks on ts.Id equals prt.TaskId into PojTsk
from t1 in PojTsk
join TL in Db.Timeline on ts.Id equals TL.TypeId into Tmln
from t2 in Tmln
join DUR in Db.Duration on ts.Id equals DUR.TypeId into Dur
from t3 in Tmln
where t1.ProjectId == ProjectId
&& t2.Type == (int)Provider.EntityType.TASK
&& t3.Type == (int)Provider.EntityType.TASK
select ts
这是我的SQL查询,我试图用C#转换为Linq:
SELECT
CONCAT('T', R1.Id) as Id,
R1.Name, R1.Description, R1.Priority,
R1.Stage, R1.Status, R1.CreatorId,
R2.ProjectId, R3.StartDate, R3.EndDate,
R3.LatestEndDate, R3.LatestStartDate,
R3.EarliestStartDate, R3.ActualStart, R3.ActualEnd,
R3.RemTime, R3.ReshowDate, R3.RemTime, R3.Completed,
R4.ActualDuration, R4.ActualDurationPlanned
FROM
(SELECT *
FROM [ProjectManagement].[dbo].[Tasks] AS TS) AS R1
JOIN
(SELECT *
FROM [ProjectManagement].[dbo].[ProjectTasks]) AS R2 ON R1.Id = R2.TaskId
LEFT JOIN
(SELECT * FROM [ProjectManagement].[dbo].[Timelines]
WHERE Type = 3) AS R3 ON R1.Id = R3.TypeId
LEFT JOIN
(SELECT * FROM [ProjectManagement].[dbo].[Durations]
WHERE Type = 3) AS R4 ON R1.Id = R4.TypeId
WHERE
ProjectId = 1
答案 0 :(得分:0)
即使仍然无法确定应该首先执行哪个查询,我已经找到了查询执行步骤:
(1)具有ProjectTasks表的内连接任务表,其中ProjectId = 1,
(2)左连接(1)到时间轴表,其中Type = 3,
(3)左连接(2)到持续时间表,其中Type = 3。
使用LINQ给出SQL查询的最近方法(我既没有测试LINQPad也没有直接测试VS):
// first inner join
var query = (from R1 in Tasks
join R2 in ProjectTasks on R1.Id equals R2.TaskId into Group1
// second left join
from T1 in Group1.DefaultIfEmpty()
join R3 in (from t in Timelines where t.Type == 3 select t) on T1.Id equals R3.TypeId into Group2
// third left join
from T2 in Group2.DefaultIfEmpty()
join R4 in (from d in Durations where d.Type == 3 select d) on T2.Id equals R4.TypeId
// still not sure to determine where condition here...
where T2.ProjectId == 1 // join result condition
select new {
Id = T1.Id.ToString().Concat("T"), // assume Tasks.Id is an int identity column converted to String value
Name = T1.Name,
Description = T1.Description,
Priority = T1.Priority,
Stage = T1.Stage,
Status = T1.Status,
CreatorId = T1.CreatorId,
ProjectId = T1.ProjectId,
StartDate = T2.StartDate,
EndDate = T2.EndDate,
LatestEndDate = T2.LatestEndDate,
EarliestStartDate = T2.EarliestStartDate,
ActualStart = T2.ActualStart,
ActualEnd = T2.ActualEnd,
RemTime = T2.RemTime,
ReshowDate = T2.ReshowDate,
Completed = T2.Completed,
ActualDuration = R4.ActualDuration,
ActualDurationPlanned = R4.ActualDurationPlanned
}).ToList();
CMIIW。
答案 1 :(得分:0)
试试这个:
from ts in Db.Tasks
join prt in Db.ProjectTasks on ts.Id equals prt.TaskId
from tl in Db.Timeline.Where(x => (x.TypeId == ts.Id)
&& (x.Type == (int)Provider.EntityType.TASK))
.DefaultIfEmpty()
from dur in Db.Duration.Where(x => (x.TypeId == ts.Id)
&& (x.Type == (int)Provider.EntityType.TASK))
.DefaultIfEmpty()
where (prt.ProjectId == ProjectId)
select new {
Id = "T" + ts.Id,
ts.Name,
ts.Description,
//...
prt.ProjectId,
tl.StartDate,
tl.EndDate,
tl.LatestEndDate,
//...
dur.ActualDuration,
dur.ActualDurationPlanned
}
from x in y.DefaultIfEmpty()
是一种创建LEFT JOIN
的简单方法。
编辑:更改了左连接中的.Where()
。