如何在VB.Net LINQ中编写此查询

时间:2017-10-09 15:18:18

标签: vb.net linq

无法弄清楚如何在VB中的LINQ中编写此查询。

SELECT 
    createdon, SUM(LoggedTime), SUM(cost)  
FROM TimeLedger 
WHERE userid = v_UserId and YEAR(createdon) = v_Year  
GROUP BY createdon 
ORDER BY createdon DESC;

这是我的第一次努力

result = (from p in db.TimeLedgers where p.userid=v_userid and year(p.createdon)= p_year group by createdon order by createdon select cost).sum()

2 个答案:

答案 0 :(得分:1)

GroupBy语法在LINQ中有点奇怪。我没有办法测试这个,但试试这个......

Dim result = (From p In db.TimeLedgers Where p.userid = v_userID AndAlso p.createdon.Year = p_year
                      Group By p.createdon Into Group
                      Order By p.createdon Descending
                      Select createdon, TotalTime = Group.Sum(Function(v) v.LoggedTime), TotalCost = Group.Sum(Function(v) v.cost))

答案 1 :(得分:1)

使用适当的VB语法和推荐的排序,

Dim result = From p In db.TimeLedgers Where p.userid = v_userID And p.createdon.Year = p_year
             Group By p.createdon Into Group
             Order By createdon Descending
             Select createdon, TotalTime = Group.Sum(Function(tl) tl.LoggedTime), TotalCost = Group.Sum(Function(tl) tl.cost)