Linq查询以获取最新的eact缺陷ID

时间:2018-03-15 09:30:00

标签: c# entity-framework linq

al_jira_defect_out 表中,我有 Batch_Insert_Timestamp AL_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_IDs重复的记录。 但是我需要每个d.AL_Defect_ID的最新记录[Max(d.Batch_Insert_Timestamp)]和所有必需的列 任何人都可以帮我获得linq查询

1 个答案:

答案 0 :(得分:1)

如果您对AL_Defect_ID进行分组,则可以按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

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