实体框架-GroupBy、OrderBy、Sum



我正试图在实体框架(方法语法(中重写此查询,但我做不到。我不明白首先需要做什么。

我的Postgres查询:

select id, Sum(yellow_hour) "yellow_hour"
from my_table
group by id
where date_part('day', date::timestamptz - '2022-10-30 00:00:00.000 +0200'::timestamptz) > 0
order by yellow_hour desc 
limit 20

我的尝试:

var sensors = dbContext.mytable
.Where(x => x.Date >= instantStartDate && x.Date <= instantEndDate)
.GroupBy(x => x.id)
.ToList();

错误:

无法转换给定的"GroupBy"模式。在"GroupBy"之前调用"AsEnumerable"以在客户端评估它。

虽然现在无法访问编译器,但可能会有一些拼写错误,但您会对当前的LINQ查询有所了解。

var queryDate = DateTime.Parse("2022-10-30 00:00:00.000 +0200");
db.my_table.Where(t=>t.date > queryDate).GroupBy(t => t.id).
Select(g => new 
{
g.Key, 
SUM = g.Sum(s=>s.yellow_hour)
}).OrderByDescending(g=>g.SUM).Take(20);

最新更新