直接上例子:
1. Group
// table1按 CaseId Group后,選擇 CaseId 和 對應的最大 Id
var qGrp = table1 .GroupBy(o => o.CaseId).Select(o => new { maxId = (o.Max(p=>p.Id)), caseId = o.Key });
2. Inner join
//table2 與上面的結果進行inner join ,得到每一個CaseId 的最大 Id的記錄
var qInner= table2.Join(
qGrp ,
outer => new { k1 = outer.CaseId, k2 = outer.IEPId },
inner => new { k1 = inner.caseId, k2 = inner.maxId },
(outer, inner) => new
{
CaseId = outer.CaseId,
CaseNo = outer.CaseNo,
CtrCode = outer.CtrCode
});