首页 新闻 会员 周边 捐助

lambda 分组过滤

0
悬赏园豆:10 [已解决问题] 解决于 2017-09-08 15:23
 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 对应的数据,其他的都过滤掉。

 

pengbg的主页 pengbg | 初学一级 | 园豆:13
提问于:2017-08-28 17:27
< >
分享
最佳答案
0

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));
}

pengbg | 初学一级 |园豆:13 | 2017-08-28 18:30
其他回答(1)
0

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();

收获园豆:10
hsliuyl | 园豆:254 (菜鸟二级) | 2017-08-28 22:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册