簡體   English   中英

使用左連接子查詢的SQL到LINQ

[英]SQL to LINQ with left join subqueries

我是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

看看下面的代碼有幫助。 我不認為這是100%正確,但接近

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; }
    }




}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM