c# - How to get the max timestamp and groupby another field in LINQ to Entities -


i have entity following fields guid taskid, guid subtaskid, datetime timestamp

the taskid may one-to-many subtaskid. it's composite key almost.

i write expression or expressions resulting in newest among each taskid i.e.

taskid subtaskid timestamp
1 2010-11-22 15:48:49.727
1 b 2010-11-22 16:24:51.117
2 c 2010-11-15 11:49:05.717
2 d 2010-11-15 14:02:11.467

would result in sequence containing only:
taskid subtaskid timestamp
1 b 2010-11-22 16:24:51.117
2 d 2010-11-15 14:02:11.467

this expression works fine showing taskid's , timestamps

var q1 =      req in myentity     orderby req.taskid     select new     {         req.taskid,         req.subtaskid,         req.timestamp     }; 

but show latest timestamp each taskid. attempts @ adding max expression fail. posting (http://stackoverflow.com/questions/2034800/how-do-you-find-the-group-wise-max-in-linq) seemed close, couldn't translate working. perhaps foreach needed.

how this?

var q1 =      req in myentity     group req req.taskid g     let topreq = g.orderbydescending(r => r.timestamp).firstordefault()     select new     {         topreq.taskid,         topreq.subtaskid,         topreq.timestamp     }; 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -