C# · 12月 29, 2021

c# – LINQ to SQL GroupBy Max()lambda

请帮我把它转换为lambda表达式 SELECT [UserID],MAX(Created) CreatedFROM [UserHistory]WHERE userid IN (14287)GROUP BY [UserID]

谢谢!

编辑:
这是我到目前为止所拥有的.

List<T> list; //list is populatedtable.Where(x => list.Select(y => y.ClientId).Contains( x.UserID)) .GroupBy(x => x.UserID) .Select(x => new { x.UserID,x.Created }) .ToList();

当我添加GroupBy时,我的Select表示没有x.UserID和x.Created的定义.

解决方法 干得好: var userIDs = new[] {14287,};var results =dataContext.UserHistories.Where(user => userIDs.Contains(user.userID)).GroupBy(user => user.userID).Select( userGroup => new { UserID = userGroup.Key,Created = userGroup.Max(user => user.Created),});