实体框架 LINQ - 带 Group By 的子查询
我正在尝试实现一个包含子查询的查询,该子查询本身包含分组。
我的代码基于此 的答案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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
query
不就是一个连接吗?我不确定
= iq
部分,但抱歉,我通常不使用该语法 - 在其他形式中,它将用于连接和选择。
Isn't
query
just a join?I'm not sure about
= iq
part but I don't usually use that syntax sorry - in the other form it would befor the join and the select.