我已经尝试了所有可能的方法并到处寻找。我很沮丧,因为这真的很愚蠢。我想要每个组的最大值。我有以下记录。
ID Custno paydt rcvDt Mon Tue
17 106505 2009-12-31 00:00:00.000 2012-07-11 07:15:04.523 100 3
18 106505 2009-12-31 00:00:00.000 2012-07-11 07:15:04.543 NULL NULL
19 106505 2010-12-31 00:00:00.000 2012-07-11 07:15:04.747 3 3
20 106505 2011-12-31 00:00:00.000 2012-07-11 07:15:04.827 3 3
结果应按 Custno 和 paydt 分组,并以最大 id 返回
ID Custno paydt rcvDt Mon Tue
18 106505 2009-12-31 00:00:00.000 2012-07-11 07:15:04.543 NULL NULL
19 106505 2010-12-31 00:00:00.000 2012-07-11 07:15:04.747 3 3
20 106505 2011-12-31 00:00:00.000 2012-07-11 07:15:04.827 3 3
尝试了不同的方式,其中一种如下
var q = from x in MyTable.ToList()
group x by new { x.custno, x.paydt } into g
select new MyTable
{
custno= g.Key.custno,
paydt= g.Key.paydt,
ID = g.Max(b => b.ID),
rcvDt = g.OrderByDescending(a => a.ID).First().rcvDt ,
mon = g.OrderByDescending(a => a.ID).First().mon,
tue = g.OrderByDescending(a => a.ID).First().tue
};
将感谢帮助!