var cartonByPartNO = (from ct in cartons
group ct by ct.PartNO
into g
select new { PartNO = g.Key, count = g.Count(),
DataCode=g.Min( x => x.DataCode),
LotNO = g.FirstOrDefault(x => x.DateCode == g.Min(v => v.DataCode))?.LotNo ,
qty = g.Sum(c => c.Qty) }).ToList();

链接