列表<t>上的LINQ查询,以根据最小值

时间:2015-09-17 10:20:54

标签: c# linq list generics generic-list

我有一个List<UserLoginRecord>对象,它由三个属性组成。

UserID  ID2      LoginTimeStamp
1234567 300009   04/08/2015 18:55:45
1234567 300009   04/08/2015 09:12:32
7654321 300010   14/08/2015 11:55:45
7654321 300010   20/08/2015 13:38:00

我想运行Linq查询以仅返回UserID和UserID2组合的第一条记录。即以下输出

UserID  ID2      LoginTimeStamp
1234567 300009   04/08/2015 09:12:32
7654321 300009   14/08/2015 11:55:45

我尝试过以下操作但遗憾的是无效。有人可以帮助我吗?

   var newList = o_userLoginList.GroupBy(gby => new {gby.UserID, gby.ID2})
                .Select(x => new UserLoginRecord
                {
                    UserID = x.UserID,
                    ID2 = x.ID2,
                    LoginTimeStamp = x.Min(LoginTimeStamp),
                });

1 个答案:

答案 0 :(得分:3)

var newList = o_userLoginList.GroupBy(gby => new {gby.UserID, gby.ID2})
                .Select(x => new UserLoginRecord
                {
                    UserID = x.Key.UserID,
                    ID2 = x.Key.ID2,
                    LoginTimeStamp = x.Min(y=>y.LoginTimeStamp),
                });