c# – LINQ to SQL GroupBy Max()lambda

前端之家收集整理的这篇文章主要介绍了c# – LINQ to SQL GroupBy Max()lambda前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请帮我把它转换为lambda表达式
SELECT [UserID],MAX(Created) Created
FROM [UserHistory]
WHERE userid IN (14287)
GROUP BY [UserID]

谢谢!

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

List<T> list;  //list is populated

table.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),});

猜你在找的C#相关文章