具有聚合函数的SQL请求到LINQ To SQL

时间:2017-08-17 18:11:03

标签: sql linq-to-sql sql-to-linq-conversion

我编写了以下基本SQL请求,它返回了所需的结果:

SELECT c.IdC, MAX(v.[Date])
FROM TableV v
JOIN TableC c ON v.IdClassified = c.IdC
WHERE v.IdUser = 'MyIdUser'
GROUP BY c.IdC
ORDER BY MAX(v.[Date]) DESC, c.IdC

我想"转换"它是LINQ,但我很难处理聚合函数和group by子句。

知道如何将其转换为LINQ吗?

1 个答案:

答案 0 :(得分:0)

找到答案:

var qViews = from v in dbContext.TableV
             join c in dbContext.TableC ON v.IdC equals c.IdC
             where v.IdUser == idUser
             group v by v.IdC into grp
             let tri = grp.Max(x => x.Date)
             orderby tri descending
             select new { x = grp.Key /*whatever else*/ };

希望有所帮助