实体框架核心上的sql查询与sum(x-y),count( )和小组

问题描述 投票:0回答:1

我得到了一个taf SQL查询,应该使用最新的postgres提供程序在最新版本的entity-framework core(dotnet core 2)中运行:

SELECT
    date_trunc('day', "StartTime") AS DAY
,   sum("AnswerTime" - "StartTime")
,   count("AnswerTime" != "ReleaseTime")
FROM "Cdrs"
GROUP BY DAY
ORDER BY DAY;

我部分可以解决它,但我无法完成整个查询。主要是计算值的总和我不知道如何解决。

还算一个条件,不知道如何在EF核心

我试图用这样的linq来解决它(不确定这是否会起作用):

[SuppressMessageAttribute("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "dateValue")]
[DbFunctionAttribute("date_trunc")]
public extern static DateTimeOffset? date_trunc(string what, DateTimeOffset? dateValue);

var x = (from c in ef.Cdrs
         group c by date_trunc("day", c.StartTime) into day
         //group c by (c.AnswerTime - c.StartTime) into g
         select new { day.Count( })

任何提示如何将SQL上面带入实体框架核心(dotnet 2)是值得赞赏的。

sql postgresql linq .net-core entity-framework-core
1个回答
0
投票

尝试类似的东西;

        var query = ef.Cdrs
            .GroupBy(x => new { Day = date_trunc("d", x.StartTime) })
            .Select(x => new
            {
                Day = x.Key.Day,
                Sum = x.Sum(s => s.AnswerTime - s.StartTime),
                Count = x.Count(c => c.AnswerTime != c.ReleaseTime)
            });
© www.soinside.com 2019 - 2024. All rights reserved.