gsiradze
gsiradze

Reputation: 4733

select max date with order by

I have this linq query

var additivesTimesTamps = (from q in Uow.UserAdditionStamps.Data
                                       group q.UserId by new
                                       {
                                           q.UserId,
                                           q.TimeStamp
                                       } into qq
                                       select new
                                       {
                                           UID = qq.Key.UserId,
                                           TimeStamp = qq.Key.TimeStamp
                                       }).ToList();

and it selects this:

1   2013-01-31 18:08:19.847 
29  2013-01-04 16:28:52.557
29  2013-01-05 16:28:52.557

How can I group that to select only 1 date per UserId? t-sql simply is this:

select userid as uId, max([timestamp]) as ts 
from UserAdditionStamp 
group by UserId

That t-sql code is exactly what i need but at linq

Upvotes: 0

Views: 277

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You have to change groupby to group only on UserId and then Select Max TimeStamp for that group like:

group q by  q.UserId into qq
                     select new
                     {
                         UID = qq.Key,
                         TimeStamp = qq.Max(x=>x.TimeStamp)
                     })

Upvotes: 2

Related Questions