实体框架 LINQ - 带 Group By 的子查询

发布于 2024-09-11 05:46:30 字数 972 浏览 5 评论 0原文

我正在尝试实现一个包含子查询的查询,该子查询本身包含分组。

我的代码基于此 的答案Question

代码的目的是根据email地址对‘person’表进行简单的去重,并返回最新的person行。

var innerQuery = (from p in db.Person
                              join r in db.Registration on p equals r.Person
                              join e in db.EventDetail on r.EventDetail equals e
                              where e.Client.ClientID == clientID
                              group p by p.Email into g
                              select g.Max(p => p.PersonID));

var query = (from p2 in db.Person where innerQuery.Contains(p2.PersonID) select p2);

当尝试执行查询时,我收到以下错误消息:

LINQ to Entities 无法识别 方法“布尔值” 包含[Int32](System.Linq.IQueryable`1[System.Int32], Int32)' 方法,并且该方法不能 被翻译成商店表达式。

我已经测试了内部查询,它只按预期返回一个整数列表,但查询失败并显示上述消息。

非常感谢任何帮助。

I am trying to achieve a query which includes a subquery which itself includes grouping.

I based my code from answers to this question

The purpose of the code is to perform a simple de-duplication of the 'person' table based on the email address and return the latest person row.

var innerQuery = (from p in db.Person
                              join r in db.Registration on p equals r.Person
                              join e in db.EventDetail on r.EventDetail equals e
                              where e.Client.ClientID == clientID
                              group p by p.Email into g
                              select g.Max(p => p.PersonID));

var query = (from p2 in db.Person where innerQuery.Contains(p2.PersonID) select p2);

When the query is attempted to execute, I get the following error message:

LINQ to Entities does not recognize
the method 'Boolean
Contains[Int32](System.Linq.IQueryable`1[System.Int32],
Int32)' method, and this method cannot
be translated into a store expression.

I have tested the innerquery and it just returns a list of ints as expected, but the query fails with the above message.

Any help greatly appreciated.

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

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

发布评论

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

评论(1

桜花祭 2024-09-18 05:46:30

query 不就是一个连接吗?

var query = from p2 in db.Person
            join iq in innerQuery on p2.PersonID equals iq
            select p2;

我不确定 = iq 部分,但抱歉,我通常不使用该语法 - 在其他形式中,它将用于

.Join(innerQuery, p2 => p2.PersonId, iq => iq, (p2, iq) => p2);

连接和选择。

Isn't query just a join?

var query = from p2 in db.Person
            join iq in innerQuery on p2.PersonID equals iq
            select p2;

I'm not sure about = iq part but I don't usually use that syntax sorry - in the other form it would be

.Join(innerQuery, p2 => p2.PersonId, iq => iq, (p2, iq) => p2);

for the join and the select.

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