LINQ中的TSQL组按查询



查看表有userid

select u.userid,review,COUNT(review) as coun,u.firstName,u.lastname,u.avatar,u.userid from reviews r
inner join users u on u.userid=r.userid
group by u.userid,review,u.firstName,u.lastname,u.avatar,u.userid
order by coun desc

这个脚本是如何翻译成linq的?我有以下不工作,不完整的代码。

var reviews = from r in context.Reviews.Where(r => r.CreateDate > dateFrom && r.CreateDate < dateTo)
                              group r by new { r.Review1, r.UserID } into g
                              join u in context.Users on g.Key.UserID equals u.UserID
                              select new UserMan
                              {
                                  ID = u.UserID,
                                  Avatar = u.Avatar,
                                  LastName = u.LastName,
                                  FirstName = u.FirstName,
                                  Review = g.Key.Review1,
                                  Reviews = g.Count()
                              };
                return reviews.ToList();

把join去掉。

var reviews = from r in context.Reviews.Where(r => r.CreateDate > dateFrom && r.CreateDate < dateTo)
    group r by new { r.Review1, r.User } into g
    select new UserMan
         {
           ID = g.Key.User.UserID,
           Avatar = g.Key.User.Avatar,
           LastName = g.Key.User.LastName,
           FirstName = g.Key.User.FirstName,
           Review = g.Key.Review1,
           Reviews = g.Count()
         };
return reviews.ToList();

相关内容

  • 没有找到相关文章

最新更新