lambda 分组过滤

 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 对应的数据,其他的都过滤掉。

 

UYOU
浏览 490回答 2
2回答

摇曳的蔷薇

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();
打开App,查看更多内容
随时随地看视频慕课网APP