【问题标题】:Linq join with last instance in tableLinq 与表中的最后一个实例连接
【发布时间】:2015-01-23 07:26:51
【问题描述】:

我有一个包含流程所有步骤的工作流程表。让我们处理其中的 2 个状态:

  1. 已保存(新项目已保存但尚未提交)
  2. 已提交(提交审核的项目)

现在我想创建一个 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 为“已保存”,我希望它给我该项目


编辑: 我有一些看起来很有效的东西。还需要再测试一下:

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

编辑: 我上面的解决方案和 Vladimir 的下面的解决方案似乎都有效,但在检查了执行计划后,Vladimir 看起来是更好的选择:

【问题讨论】:

  • 如果您不选择它,为什么还需要 workFlowHistory?您拥有的唯一条件是 workFlowHistory.WorkFlowStep == "Saved" 这对我来说没有多大意义?因此,您通过具有任何已保存 workFlowHistory 的 WasteProgrammeId 和 WasteSourceId 选择 wasteInformation?
  • 据我了解,您希望通过提供的 WasteProgrammeId 和 WasteSourceId 以及最新保存的 workFlowHistory 获得 wasteInformation 是否正确?
  • 或者您想获取在 db 中的所有 workflowHistories 中具有最大日期的 workflowHistory 的 wasteInformation?
  • @Vladimirs - 是的。每次保存废物信息时,它都会创建一个工作流条目。任何废物信息都可以有多个工作流条目。我想要来自指定程序和源的所有废物信息条目,其中有一个带有“已保存”的 WorkFlowStep 的持续工作流条目。
  • @Vladimirs - 我有一些看起来可行的东西。将其添加到问题中

标签: c# sql-server linq join


【解决方案1】:

假设您在WasteInformation 上收集了WorkFlowHistories,我相信查询将选择WasteInformations 及其最新的WorkFlowHistory(如果有):

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()
}

【讨论】:

  • 啊。谢谢。形成您的查询,我认为最好省略.Where(x => x.WorkFlowStep == "Saved"),否则它将获得具有该状态的最后一个工作流条目,而不是最后一个整体工作流条目。然后,我可以检查最后一个工作流条目的状态为“已保存”。
  • @Carel 当然,稍后在代码中您可以轻松检查它是否保存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
相关资源
最近更新 更多