List<Apple> listapple = new List<Apple>(); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(1), geely_wsordercode = "SO01", pricetypecode = "SV100", sapValue = 100 }); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(2), geely_wsordercode = "SO01", pricetypecode = "SV200", sapValue = 200 }); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(3), geely_wsordercode = "SO01", pricetypecode = "SV200", sapValue = 300 }); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(4), geely_wsordercode = "SO01", pricetypecode = "SV200", sapValue = 400 }); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(5), geely_wsordercode = "SO02", pricetypecode = "SV100", sapValue = 500 }); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(6), geely_wsordercode = "SO02", pricetypecode = "SV100", sapValue = 600 }); listapple.Add(new Apple() { dt = DateTime.Now.AddDays(7), geely_wsordercode = "SO02", pricetypecode = "SV100", sapValue = 700 });
希望用 lambda ,通过 geely_wsordercode 和 pricetypecode 分组,取最大时间的 sapValue 对应的数据,其他的都过滤掉。
var listresult= listapple.GroupBy(x => new {x.geely_wsordercode, x.pricetypecode}).ToList();
foreach (var variable in listresult)
{
var temp= variable.FirstOrDefault(x => x.dt == variable.Max(y => y.dt));
}
List<int> sapValue = listapple.Where(r => listapple.GroupBy(x => new { x.geely_wsordercode, x.pricetypecode }).Select(m => m.Max(n => n.dt)).ToList().Contains(r.dt)).Select(r => r.sapValue).ToList();