gpt4 book ai didi

c# - EF Linq to Entities Query with groupby sum 和 min

转载 作者:行者123 更新时间:2023-11-30 21:17:39 24 4
gpt4 key购买 nike

我正在尝试使 Linq to Entities Query 正常工作,但我无法弄明白。

这是我的 SQL 查询,运行良好:

SELECT SUM(ca.PointValue) as Points, ua.UserFBID, MIN(ua.[Date]) as FirstDate
FROM [LeaderOfNow].[dbo].[QuestionAnswer] ca
inner join [LeaderOfNow].[dbo].[LONUserAnswers] ua
on ca.Id = ua.AnswerId
group by ua.UserFBID
order by Points desc, FirstDate asc

到目前为止我最好的尝试是:

            var leaders = db.LONUserAnswers
.GroupBy(a => a.UserFBID)
.Select(a =>
new
{
FBID = a.Key,
CurrentPoints = a.Select(v => v.QuestionAnswer.PointValue).Sum(),
FirstAnswered = a.Min(v => v.Date)
})
.OrderByDescending(a => a.CurrentPoints)
.OrderBy(a => a.FirstAnswered)
.Take(10)
.ToList();

然而,这会导致 sql 困惑,并且只尊重第一个顺序而不是第二个顺序,这是我需要工作的。关于我做错了什么的任何建议?感谢您的帮助。

最佳答案

当您链接 OrderBy-Functions 时,您必须为后者使用 ThenBy()ThenDescendingBy()

关于c# - EF Linq to Entities Query with groupby sum 和 min,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4686228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com