LINQ Group By&最大的问题 [英] LINQ Group By & max question
问题描述
你好
我有一个清单:
ID ----- ---日期----------- NumValue
1 --------- 2/2/2013 ---------- 0
1 --------- 2/2/2013 ---------- 2
1 ------- - 2013年1月1日---------- 4
2 --------- 10/5/2009 --------- 3
2 --------- 01/01/2005 -------- 4
我''想要获得最大(不一定)日期值的所有订单项
所以我们的目标是回到第0个第1行第3行
弱尝试如下:
Hello
I''ve got a list:
ID--------Date-----------NumValue
1---------2/2/2013----------0
1---------2/2/2013----------2
1---------1/1/2013----------4
2---------10/5/2009---------3
2---------01/01/2005--------4
I''d like to get back all the line items with the max(not necessarily one) date value
so the aim is to get back 0th 1 st 3 rd line
the weak attempt as follows:
var plpo1 = (from y in plpo0
group y by new { y.tervcsop, } into grouped
let maxdate = grouped.Max(y => y.maxdate)
orderby maxdate descending
select new
{
tcs = grouped.Key.tervcsop,
md = maxdate,
norm = grouped.Select(x => x.normaertek)
}).ToList();
var plpo2 = plpo1.Where(x => x.md == plpo1.Max(o => o.md));
感谢分享想法
thanks for sharing the ideas
推荐答案
试试这一个:
Try this one:
var groups = testlist.GroupBy(a => (a.ID.ToString()));
groups.SelectMany(a => a.Where(b => b.Date == a.Max(c => c.Date)))
如果有效,请告诉我。
Let me know if it works.
这篇关于LINQ Group By&最大的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!