Ehsan Akbar Ehsan Akbar - 3 months ago 11
C# Question

Get the latest record in join linq c#

I have this query

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,
};


My
line
tables can have multi
fileIsoManager
records.So in the select statement i mean in this part
Revision = fileIsoManager.Revision
i want to show the latest record in
fileIsoManager
table .how can i change this query to do that ?

Answer

Ok, since you didn't provide much details, i've wrote a few classes to show you how to get your latest revision for each line. Pay attention to the "main" function.

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

So, basically, you need to group your revisions by line. In the select part, you can then get the latest revision for each line.

In this example, result will contain a dynamic object containing the line id=1 and the revision id = 3.

Going back to your query, it should look like this to get a similar result :

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