简体   繁体   中英

SQL to LINQ with left join subqueries

I am new to LINQ and trying to write to convert some SQL into LINQ and am not sure what is wrong in my code. I am trying to do multiple left joins with sub-queries but am not sure how to accomplish this in LINQ. I need to return a 1 if there are any results. Any help is appreciated.

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

See if code below helps. I don't think it is 100% correct, but close

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication108
{
    class Program
    {
        static void Main(string[] args)
        {
            DataBase db = new DataBase();
            int ProcessStatus = 1;
            int ProcessID = 21;
            int Status = 1;
            DateTime today = DateTime.Now.Date;
            string BankAccountID = "ABC";
            int AdjustmentMethod = 5;

            var results = (from A in db.Approved
                               .Where(x => (x.ProcessID == ProcessID) 
                                   && (x.Status == Status) 
                                   && (x.EffectiveStartDate >= today) 
                                   && (today < x.EffectiveEndDate))
                          join OA in db.OrgApproval
                               .Where(x => (x.ProcessID == ProcessID) 
                                   && (x.EffectiveStartDate >= today) 
                                   && (today < x.EffectiveEndDate) 
                                   && (x.BankAccountID == BankAccountID) 
                                   && (x.ValidatedFlag == DBNull.Value)
                                   && (x.ProcessStatus == ProcessStatus)
                                   && (x.AdjustmentMethod == AdjustmentMethod)) on A.OrgApprovalID equals OA.OrgApprovalID
                          select new { A = A, OA = OA })
                          .Where(x => (x.OA.ApprovalLevel < db.OrgApproval.Where(y => x.OA.OrgStructureID == y.OrgStructureID).Max(z => z.ApprovalLevel)) && (x.A.ApprovalLevel == x.OA.ApprovalLevel))
                          .GroupBy(x => x.OA.OrgStructureID)
                          .SelectMany(x =>  x.Select(y => new { RecordIDKey = y.A.RecordIDKey, ApprovalLevel = y.A.ApprovalLevel}))
                          .ToList();

        }

    }
    public class DataBase
    {
        public List<Approved> Approved { get; set; }
        public List<OrgApproval> OrgApproval { get; set; } 
    }
    public class Approved
    {
        public string OrgApprovalID { get; set; }
        public int ProcessID { get; set; }
        public int Status { get; set; }
        public DateTime EffectiveStartDate { get; set; }
        public DateTime EffectiveEndDate { get; set; }

        public string RecordIDKey { get; set; }
        public int ApprovalLevel { get; set; }
    }
    public class OrgApproval
    {
        public int ProcessID { get; set; }
        public string OrgApprovalID { get; set; }
        public string  OrgStructureID { get; set; }
        public int ApprovalLevel { get; set; }
        public string NumberofApprovalRequired { get; set; }
        public string BankAccountID { get; set; }
        public object ValidatedFlag { get; set; }
        public int ProcessStatus { get; set; }
        public int AdjustmentMethod { get; set; }
        public int CurrentLevel { get; set; }


        public DateTime EffectiveStartDate { get; set; }
        public DateTime EffectiveEndDate { get; set; }
    }




}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM