如何使用LINQ中的Max翻译我的SQL查询?

发布于 2025-01-29 21:00:36 字数 1092 浏览 4 评论 0原文

我想用EF在LINQ中翻译此SQL查询,

SELECT Agts.AgtNum, Agts.AgtLastname, Agts.AgtFirstname, COUNT(Co.CoEnd) FROM [dbo].Agts AS Agts
INNER JOIN [dbo].[Contracts] AS Co ON Agts.AgtNum = Co.AgtNum
GROUP BY Agts.AgtNum, Agts.AgtLastname, Agts.Firstname
HAVING MAX(Co.CoEnd) <= '2020-05-17'
ORDER BY AgtNum asc

我尝试了:

    public List<AgentToPurge> AgentsToPurge(DateTime datePurge)
    {
        return  (from agent in this.Entities.Agts
                join contract in this.Entities.Contracts on agent.AgtNum equals contract.AgtNum
                group agent by agent.AgtNum into g
                where g.CoEnd <= datePurge

                select new AgentToPurge
                {
                    Id = g.Key,
                    Lastname = g.Key.AgtLastname,
                    Firstname = g.Key.AgtFirstname,
                    Contract_Deleted = g.Key.CoEnd.Count()
                }).ToList();
    }

但是行

where g.CoFin <= datePurge

不起作用。

我认为我的“选择新”也不正确。

您能帮我解决这个问题吗?

I'd like to translate this SQL Query in LINQ with EF

SELECT Agts.AgtNum, Agts.AgtLastname, Agts.AgtFirstname, COUNT(Co.CoEnd) FROM [dbo].Agts AS Agts
INNER JOIN [dbo].[Contracts] AS Co ON Agts.AgtNum = Co.AgtNum
GROUP BY Agts.AgtNum, Agts.AgtLastname, Agts.Firstname
HAVING MAX(Co.CoEnd) <= '2020-05-17'
ORDER BY AgtNum asc

I tried that :

    public List<AgentToPurge> AgentsToPurge(DateTime datePurge)
    {
        return  (from agent in this.Entities.Agts
                join contract in this.Entities.Contracts on agent.AgtNum equals contract.AgtNum
                group agent by agent.AgtNum into g
                where g.CoEnd <= datePurge

                select new AgentToPurge
                {
                    Id = g.Key,
                    Lastname = g.Key.AgtLastname,
                    Firstname = g.Key.AgtFirstname,
                    Contract_Deleted = g.Key.CoEnd.Count()
                }).ToList();
    }

But the line

where g.CoFin <= datePurge

doesn't work.

I think my "select new" isn't correct either.

Could you help me to solve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2025-02-05 21:00:36

尝试以下查询:

public List<AgentToPurge> AgentsToPurge(DateTime datePurge)
{
    return  (from agent in this.Entities.Agts
            join contract in this.Entities.Contracts on agent.AgtNum equals contract.AgtNum
            group contract by new { agent.AgtNum, agent.AgtLastname, agent.AgtFirstname } into g
            where g.Max(x => x.CoEnd) <= datePurge
            select new AgentToPurge
            {
                Id = g.Key.AgtNum,
                Lastname = g.Key.AgtLastname,
                Firstname = g.Key.AgtFirstname,
                Contract_Deleted = g.Sum(x => x.CoEnd != null ? 1 : 0)
            }).ToList();
}

请注意,LINQ查询是从类和导航属性构建的,如果您具有正确定义的模型,则可能不需要加入。

Try the following query:

public List<AgentToPurge> AgentsToPurge(DateTime datePurge)
{
    return  (from agent in this.Entities.Agts
            join contract in this.Entities.Contracts on agent.AgtNum equals contract.AgtNum
            group contract by new { agent.AgtNum, agent.AgtLastname, agent.AgtFirstname } into g
            where g.Max(x => x.CoEnd) <= datePurge
            select new AgentToPurge
            {
                Id = g.Key.AgtNum,
                Lastname = g.Key.AgtLastname,
                Firstname = g.Key.AgtFirstname,
                Contract_Deleted = g.Sum(x => x.CoEnd != null ? 1 : 0)
            }).ToList();
}

Note that LINQ query is built from classes and navigation properties and probably you will not need JOIN, if you have properly defined Model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文