Click here to Skip to main content
14,664,816 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted
Comments
Michiel du Toit 15-Feb-13 5:55am
   
Please improve your question. You are already getting all items with the maximum values with your plpo2 assignment, what exactly do you want to do in addition?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Comments
gericooper 15-Feb-13 7:10am
   
Awesome stuff, thank you....Will be studying SelectMany....
Motamas 15-Feb-13 8:18am
   
It is really useful :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100