Я новичок в LINQ и пытаюсь написать, чтобы преобразовать некоторый SQL в LINQ, и я не уверен, что не так в моем коде.Я пытаюсь сделать несколько левых соединений с подзапросами, но я не уверен, как это сделать в LINQ.Мне нужно вернуть 1, если есть какие-либо результаты.Любая помощь приветствуется.
SQL
SELECT 1
FROM FM.SAAdjustment SAA
LEFT JOIN
(select RecordIDKey,
ApprovalLevel,
NumberOfApprovals = count(ApprovedID)
from dbo.Approved A
JOIN dbo.OrgApproval OA ON A.OrgApprovalID = OA.OrgApprovalID
where A.ProcessID = 21
and A.Status = 1
and convert(varchar(32),GetDate(),101) between OA.EffectiveStartDate AND OA.EffectiveEndDate
group by RecordIDKey, ApprovalLevel
) A ON SAA.SAAdjustmentID = A.RecordIDKey and SAA.CurrentLevel = A.ApprovalLevel
LEFT JOIN
(select OrgStructureID,
ApprovalLevel,
NumberofApprovalRequired
from dbo.OrgApproval
where ProcessID = 21
and convert(varchar(32),GetDate(),101) between EffectiveStartDate AND EffectiveEndDate
) OA on SAA.OrgStructureID = OA.OrgStructureID and SAA.CurrentLevel = OA.ApprovalLevel
LEFT JOIN
(select OrgStructureID,
HighestApprovalLevel = max(ApprovalLevel)
from dbo.OrgApproval
where ProcessID = 21
and convert(varchar(32),GetDate(),101) between EffectiveStartDate AND EffectiveEndDate
group by OrgStructureID
) MA ON SAA.OrgStructureID = MA.OrgStructureID
WHERE SAA.BankAccountID = @BankAccountID
and SAA.ProcessStatus = 1
and SAA.AdjustmentMethod = 5
and isnull(SAA.ValidatedFlag,0) = 1
and (SAA.CurrentLevel < MA.HighestApprovalLevel
or SAA.CurrentLevel = HighestApprovalLevel and isnull(A.NumberOfApprovals,0) < OA.NumberofApprovalRequired
)
LINQ
var today == DateTime.Now
from SAA in SAAdjustment
join Asub in
(
from App in Approved
join OA in OrgApproval on A.OrgApprovalID equals OA.OrgApprovalID
where App.ProcessID == 21 && App.Status == 1
group App by new {App.RecordIDKey, App.ApprovalLevel} into AG
select AG, NumberofApprovals == ApprovedID.Count()
) on SAA.AdjustmentID equals App.RecordIDKey && SAA.CurrentLevel equals App.ApprovalLevel into AGroup
from A in Asub.DefaultIfEmpty()
join OAsub in
(
from OA in OrgApproval
where ProcessID == 21 && today >= EffectiveStartDateDate && today <= EffectiveEndDate
select OrgStructureID, APprovalLevel, NumberofApprovalRequired
) on SAA.OrgStructureID equals OA.OrgStructureID && SAA.CurrentLevel equals OA.ApprovalLevel into OAGroup
from OApp in OAsub.DefaultIfEmpty()
join MAsub in
(
from MA in OrgApproval
where ProcessID == 21 && today >= EffectiveStartDateDate && today <= EffectiveEndDate
group MA by OrgStructureID
select OrgStructureID, HighestApprovalLevel == ApprovalLevel.Max()
) on SAA.OrgStructureID equals MA.OrgStructureID
from MApp in MAsub.DefaultIfEmpty()
where SAA.BankAccountID == bankAccountId
&& SAA.ProcessStatus == 1
&& SAA.AdjustmentMethod == 5
&& (SAA.ValidatedFlag == null || (SAA.ValidatedFlag.HasValue && SAA.ValidatedFlag.Value == false))
&& (SAA.CurrentLevel < MA.HighestApprovalLevel || (SAA.CurrentLevel == SAA.HighestApprovalLevel && App.NumberOfApprovals == null) < OA.NumberofApprovalRequired
select 1