I have a master-detail tables and I want to get list of master join with detial where detail is max in some filed. for example I have a table named Document and also a child table named Revision .I want to get list of document join Revision where Revision filed is max ? One solution is:
using ( ProcurementDataContext dc = new ProcurementDataContext() )
{
var temp = from ddr in dc.E_DesignDocumentRevisions
group ddr by ddr.DesignDocumentID into g
select new { MaxRevision = g.Max(x => x.Revision), g.Key };
var result = from t in temp
join ddr in dc.E_DesignDocumentRevisions
on new { DesignDocumentID = t.Key, Revision = t.MaxRevision } equals new { ddr.DesignDocumentID, ddr.Revision }
join dd in dc.E_DesignDocuments
on ddr.DesignDocumentID equals dd.ID
where dd.DesignDocumentTypeID == DesignDocumentTypes.MR
select new { ddr.ID, dd.DocumentNumber };
foreach (var item in result )
{
dic.Add(item.ID, item.DocumentNumber.ToString());
}
}
How can I do that in just one query?