Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq与表中的最后一个实例连接_C#_Sql Server_Linq_Join - Fatal编程技术网

C# Linq与表中的最后一个实例连接

C# Linq与表中的最后一个实例连接,c#,sql-server,linq,join,C#,Sql Server,Linq,Join,我有一个工作流表,它包含流程的所有步骤。让我们使用其中的两种状态: 已保存(新项目已保存但尚未提交) 已提交(提交审查的项目) 现在我想创建一个BatchSumbit函数,它将提交所有未提交的项目。为此,我需要查询最新工作流状态为“已保存”的所有项目。项目的所有历史工作流条目仍然存在,并且可以从“已提交”返回到“已保存”几次 以下是表格结构: 现在我想要一个linq查询,它将提供我所需要的: from wasteInformation in wasteDB.WasteInformations

我有一个工作流表,它包含流程的所有步骤。让我们使用其中的两种状态:

  • 已保存(新项目已保存但尚未提交)
  • 已提交(提交审查的项目)
  • 现在我想创建一个BatchSumbit函数,它将提交所有未提交的项目。为此,我需要查询最新工作流状态为“已保存”的所有项目。项目的所有历史工作流条目仍然存在,并且可以从“已提交”返回到“已保存”几次

    以下是表格结构:

    现在我想要一个linq查询,它将提供我所需要的:

    from wasteInformation in wasteDB.WasteInformations
    join workFlowHistory in wasteDB.WorkFlowHistories on wasteInformation.WasteInformationId equals workFlowHistory.WasteInformationId
    // Join with last instance in workflow table (where workflowHistory.DateAdded is greatest)
    where workFlowHistory.WorkFlowStep == "Saved"
          && wasteInformation.WasteProgrammeId == captureModel.WasteProgrammeId
          && wasteInformation.WasteSourceId == captureModel.WasteSourceId
    select new
    {
        WasteInformationId = wasteInformation.WasteInformationId,
        FinancialQuarter = wasteInformation.FinancialQuarter,
        FinancialYear = wasteInformation.FinancialYear,
        WasteProgrammeId = wasteInformation.WasteProgrammeId,
        WasteMonth = wasteInformation.WasteMonth,
        WasteYear = wasteInformation.WasteYear,
        DateCaptured = wasteInformation.DateCaptured,
        WasteSourceId = wasteInformation.WasteSourceId,
        WasteDate = wasteInformation.WasteDate
    }
    
    查询将给出该项的所有已保存条目。如果项目的最后一个条目的WorkFlowStep为“Saved”,我希望它为我提供该项目


    编辑: 我有一些东西看起来很管用。还需要进一步测试:

    var SavedWasteInformation = wasteDB.WasteInformations.Where(wi => wi.WorkFlowHistories.FirstOrDefault(wf => wf.DateAdded == wi.WorkFlowHistories.Max(wf_in => wf_in.DateAdded)).WorkFlowStep == "Saved"
                                && wi.WasteProgrammeId == captureModel.WasteProgrammeId
                                && wi.WasteSourceId == captureModel.WasteSourceId);
    

    编辑: 我上面的解决方案和弗拉基米尔下面的解决方案似乎都有效,但在检查了执行计划后,弗拉基米尔的方案似乎是更好的选择:


    如果您在
    废物信息上收集了
    工作流历史记录
    ,我相信查询将选择
    废物信息
    及其最新的
    工作流历史记录
    (如果有):


    如果不选择workFlowHistory,为什么需要它?您唯一的条件是workFlowHistory.WorkFlowStep==“已保存”,这对我来说没有太多意义?因此,您选择了wasteInformation by WasteProgrammeId和WasteSourceId,其中包含任何已保存的工作流历史记录?据我所知,您希望WasteProgrammeId和WasteSourceId提供的wasteInformation及其最新保存的工作流历史记录正确吗?或者您希望获取具有所有工作流历史记录中日期最长的wasteInformationdb中的工作流历史记录?@Vladimirs-是的。每次保存信息时,它也会创建一个工作流条目。任何信息都可以有多个工作流条目。我想要程序中的所有信息条目和指定的源,其中有一个lastes工作流条目,WorkFlowStep为“Saved”。@Vladimirs-我得到了一些看起来有效的东西。将其添加到问题中。谢谢从您的查询中,我认为最好省去
    。其中(x=>x.WorkFlowStep==“Saved”)
    ,否则它将获得具有该状态的最后一个工作流条目,而不是最后一个整体工作流条目。之后,我可以检查最后一个工作流条目的状态为“已保存”。@Carel当然,稍后在代码中,您可以轻松检查它是否已保存。
    from wasteInformation in wasteDB.WasteInformations
    where wasteInformation.WasteProgrammeId == captureModel.WasteProgrammeId
          && wasteInformation.WasteSourceId == captureModel.WasteSourceId
    select new
    {
        WasteInformation = wasteInformation,
        LastSavedWorkFlowHistory = wasteInformation.WorkFlowHistories
            .Where(x => x.WorkFlowStep == "Saved")
            .OrderByDescending(x => x.DateAdded)
            .FirstOrDefalt()
    }