获取加入linq c#的最新记录

时间:2016-09-01 17:14:11

标签: c# linq max

我有这个查询

return from fitUpExaminationDetails in _ctx.FitUpExaminationDetails
                where fitUpExaminationDetails.FitUpExaminationId == Id
                join joint in _ctx.Joints on fitUpExaminationDetails.JointId equals joint.Id
                join line in _ctx.Lines on joint.LineId equals line.Id
                join fileIsoManager in _ctx.FileIsoManagers on line.Id equals fileIsoManager.LineId

                select new ViewDomainClass.QualityControl.Report.ViewFitupExaminationReport
                {

                    HeatNumber1 = fitUpExaminationDetails.HeatNumber1,
                    HeatNumber2 = fitUpExaminationDetails.HeatNumber2,
                    JointNumber = joint.JointNumber,
                    LineNumber = line.LineNumber,
                    Revision = fileIsoManager.Revision,            
                };

我的line表可以包含多个fileIsoManager条记录。所以在select语句中我的意思是这部分Revision = fileIsoManager.Revision我希望在fileIsoManager表中显示最新记录。如何更改此查询才能执行此操作?

1 个答案:

答案 0 :(得分:3)

好的,既然你没有提供太多细节,我写了一些课程,向你展示如何获得每行的最新修订版。注意“主要”功能。

// These are a representation of your classes :
public class Line
{
    public int LineId { get; set; }
    public string SomeValue { get; set;}
}

public class Revision
{
    public int RevisionId { get; set; }
    public int LineId { get; set;}  
}

void Main()
{
    // generating some data so we can test the query.
    var lines = new List<Line>() { 
        new Line() { LineId = 1, SomeValue = "Allo!" } 
        };

    var revisions = new List<Revision>() { 
        new Revision() { LineId = 1, RevisionId = 1 }, 
        new Revision() { LineId = 1, RevisionId = 2 }, 
        new Revision() { LineId = 1, RevisionId = 3 }
        };


    var result = (
        from line in lines
        join revision in revisions on line.LineId equals revision.LineId
        group revision by line into grp
        select new
        {
            Line = grp.Key,
            LastRevision = grp.OrderByDescending(rev => rev.RevisionId).FirstOrDefault()
        }
        ).ToList();
}

因此,基本上,您需要按行对修订进行分组。在选择部分中,您可以获得每行的最新修订版。

在此示例中,result将包含一个动态对象,其中包含行id = 1和修订版ID = 3.

回到你的查询,它应该看起来像这样得到类似的结果:

return 
    from fitUpExaminationDetails in _ctx.FitUpExaminationDetails
    where fitUpExaminationDetails.FitUpExaminationId == Id
    join joint in _ctx.Joints on fitUpExaminationDetails.JointId equals joint.Id
    join line in _ctx.Lines on joint.LineId equals line.Id
    join fileIsoManager in _ctx.FileIsoManagers on line.Id equals fileIsoManager.LineId
    group fileIsoManager by new { fitUpExaminationDetails, joint, line} into grp
    select new ViewDomainClass.QualityControl.Report.ViewFitupExaminationReport
    {

       HeatNumber1 = grp.Key.fitUpExaminationDetails.HeatNumber1,
       HeatNumber2 = grp.Key.fitUpExaminationDetails.HeatNumber2,
       JointNumber = grp.Key.joint.JointNumber,
       LineNumber = grp.Key.line.LineNumber,
       Revision = grp.OrderByDescending(fileIsoMgr => gileIsoMgr.Id).FirstOrDefault()
    };