【问题标题】:Linq Query to get latest of eact defect IDLinq 查询以获取最新的 eact 缺陷 ID
【发布时间】:2018-08-24 01:01:08
【问题描述】:

al_jira_defect_out 表中,我有 Batch_Insert_TimestampAL_Defect_ID 列以及其他列。在此表中,将有具有不同 Batch_Insert_Timestamp 的重复 AL_Defect_ID .

根据 linq 中的以下代码,我正在获取 Jira_Status == 0 的所有记录

var defects = (from d in db.al_jira_defect_out
    join p in db.al_jira_ref_defect_project on d.Defect_Product_ID.ToString() equals p.al_product_id
    join t in db.al_jira_ref_defect_type on d.Defect_Type_ID equals t.type
    where d.Jira_Status == 0
    select new Issue
    {
        fields = new CreateIssue
        {
            project = new Project
            {
                key = g.jira_project_key
            },
            issuetype = new Issuetype()
            {
                name = t.jira_equivalent
            },
            description = d.Defect_ShortDesc,
            summary = d.Defect_DetailDesc,
            versions = new List<Versions>
            {
                new Versions {name = d.Defect_Release_DetectedIn}
            },
            priority = new Priority
            {
                name = d.Defect_Severity_Name
            },
            customfield_10182 = d.AL_Defect_ID.ToString(),
        }
    }).Select().ToList();

我收到了 AL_Defect_ID 重复的记录。 但是我需要每个 d.AL_Defect_ID 的最新记录 [Max(d.Batch_Insert_Timestamp)] 以及所有必需的列 谁能帮我获取 linq 查询

【问题讨论】:

  • 你可以使用groupby,然后在select语句中取max、last、sum等...
  • 您正在使用 OR-mapper:不要让数据库名称渗透到 C# 类模型中。命名约定的存在是有原因的。另外,不要加入,使用导航属性。这样做,它可以像select Project.Defects.OrderByDescending(d =&gt; d.TimeStamp).FirstOrDefault() 一样简单。

标签: c# entity-framework linq


【解决方案1】:

如果你在AL_Defect_IDs 上分组,你可以通过Batch_Insert_Timestamp 订购并拿最后一个(最新的):

var defects = (from d in db.al_jira_defect_out
               join p in db.al_jira_ref_defect_project on d.Defect_Product_ID.ToString() equals p.al_product_id
               join t in db.al_jira_ref_defect_type on d.Defect_Type_ID equals t.type
               where d.Jira_Status == 0
               group new { d, p, t } on d.AL_Defect_ID into dptg
               let dpt = (from dpt in dptg order by dpt.d.Batch_Insert_Timestamp select dpt).Last()
               select new // ... whatever using dpt

您的示例代码在select 中没有使用p,因此您可以将其排除在grouping 之外。

【讨论】:

  • Tq @NetMage。暂时我已经创建了存储过程。我会在有空的时候尝试一下,如果我有任何疑问,请告诉您
猜你喜欢
  • 2021-06-26
  • 1970-01-01
  • 2021-03-19
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多