在 linq toEntity 中,如何执行内部 EXISTS (SELECT 1 FROM... 类型的查询?
我正在尝试编写与使用 EF CTP 5 等效的 LINQ
SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
EXISTS (SELECT 1 FROM T2
WHERE T1.C17 = T2.C24)
ORDER BY C3
,因此我有一个名为 dbc 的 DBContext 变量,其中包括基于 POCO T1 和 T2 的 DBSet 对象 T1 和 T2。
在 LINQ 中,我编写
DIM IND = From i In dbc.T1s
Where i.C4 = "xyz"
And (From t In dbc.T2s Where i.C17 = t.C24).Any
Select i.C1, i.C2, i.C3
Order By C3
运行查询,收到错误消息“无法创建类型‘T2’的常量值。在此上下文中仅支持原始类型(‘例如 Int32、String 和 Guid’)。”
当我省略内部表达式(LINQ 代码中的第三行)时,查询运行良好。
我尝试将内部比较的顺序切换为 t.C24 = i.C17,但没有效果。
那么这是怎么回事,我该如何解决呢?
I am trying to write a LINQ equivalent of
SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
EXISTS (SELECT 1 FROM T2
WHERE T1.C17 = T2.C24)
ORDER BY C3
I'm using EF CTP 5, so I have a DBContext variable named dbc, which includes DBSet objects T1s and T2s, based on POCOs T1 and T2.
In LINQ I write
DIM IND = From i In dbc.T1s
Where i.C4 = "xyz"
And (From t In dbc.T2s Where i.C17 = t.C24).Any
Select i.C1, i.C2, i.C3
Order By C3
Running the query I get the error message "Unable to create a constant value of type 'T2'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."
When I omit the inner expression (third line in the LINQ code), the query runs fine.
I tried switching the orders of the inner comparison, to be t.C24 = i.C17, with no effect.
So what is going on, and how can i fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将查询更改为使用 LINQ to SQL,这样 dbc 就是一个 DataContext 对象,一切都很好。看来 LINQ to Entities 仍然(即使在 .Net 4 中)相当有限。
I changed the query to use LINQ to SQL such that dbc is a DataContext object, and all is fine. Seems LINQ to Entities is still (even in .Net 4) quite limited.