来自包含表的 IN 子句未按预期工作
我正在尝试这样做:
var results = (from s in db.ExampleMaster
.Include("State")
.Include("Group")
.Where("it.Group.IdGroup in {1,2,3,4,5}")
.Where("it.IdState in {1,2}")
select s);
架构:
ExampleMaster:
IdExampleMaster int,
IdState int
State:
IdState int,
Description varchar(100)
Group:
IdGroup int
IdExampleMaster int
所需的 SQL 类似于:
SELECT * FROM ExampleMaster e
inner join State s on s.IdState = e.IdState
Inner join Group g on e.IdExampleMaster = g.IdExampleMaster
where g.IdGroup in (1,2,3,4,5) and e.IdState in (1,2)
生成的 SQL 类似于:
SELECT *
FROM ExampleMaster AS [Extent1]
LEFT OUTER JOIN [dbo].[Group] AS [Extent2] ON [Extent1].[IdExampleMaster] = [Extent2].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent3] ON [Extent2].[IdGroup] = [Extent3].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent4] ON [Extent2].[IdGroup] = [Extent4].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent5] ON [Extent2].[IdGroup] = [Extent5].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent6] ON [Extent2].[IdGroup] = [Extent6].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent7] ON [Extent2].[IdGroup] = [Extent7].[IdGroup]
LEFT OUTER JOIN [dbo].[State] AS [Extent8] ON [Extent1].[IdState] = [Extent8].[IdState]
WHERE ([Extent1].[IdState] IN (1,2)) AND ([Extent3].[IdGroup] = 1 OR [Extent4].[IdGroup] = 2 OR [Extent5].[IdGroup] = 3 OR [Extent6].[IdGroup] = 4 OR [Extent7].[IdGroup] = 5) ) AS [Filter1]
我不明白为什么状态情况(不需要包含表)与 IN 子句配合良好,并且组进行了如此多的连接比列表中的项目要多,甚至它没有按预期工作。
有什么想法吗?
已编辑
我在我的数据库架构上发现了一个错误,现在我得到的错误
.Where("it.Group.IdGroup in {1,2,3,4,5}")
是
“IdGroup”不是“Transient.collection[myModel.Group(Nullable=True,DefaultValue=)]”的成员。要提取集合元素的属性,请使用子查询迭代集合
I'm trying to do this:
var results = (from s in db.ExampleMaster
.Include("State")
.Include("Group")
.Where("it.Group.IdGroup in {1,2,3,4,5}")
.Where("it.IdState in {1,2}")
select s);
The Schema:
ExampleMaster:
IdExampleMaster int,
IdState int
State:
IdState int,
Description varchar(100)
Group:
IdGroup int
IdExampleMaster int
The SQL Desired is like:
SELECT * FROM ExampleMaster e
inner join State s on s.IdState = e.IdState
Inner join Group g on e.IdExampleMaster = g.IdExampleMaster
where g.IdGroup in (1,2,3,4,5) and e.IdState in (1,2)
The SQL Generated is like:
SELECT *
FROM ExampleMaster AS [Extent1]
LEFT OUTER JOIN [dbo].[Group] AS [Extent2] ON [Extent1].[IdExampleMaster] = [Extent2].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent3] ON [Extent2].[IdGroup] = [Extent3].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent4] ON [Extent2].[IdGroup] = [Extent4].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent5] ON [Extent2].[IdGroup] = [Extent5].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent6] ON [Extent2].[IdGroup] = [Extent6].[IdGroup]
LEFT OUTER JOIN [dbo].[Group] AS [Extent7] ON [Extent2].[IdGroup] = [Extent7].[IdGroup]
LEFT OUTER JOIN [dbo].[State] AS [Extent8] ON [Extent1].[IdState] = [Extent8].[IdState]
WHERE ([Extent1].[IdState] IN (1,2)) AND ([Extent3].[IdGroup] = 1 OR [Extent4].[IdGroup] = 2 OR [Extent5].[IdGroup] = 3 OR [Extent6].[IdGroup] = 4 OR [Extent7].[IdGroup] = 5) ) AS [Filter1]
I don't understand why the state case (no Included table required) works well with IN clause and the Group make so many joins than items on the list and even that it doesn't works as expected.
Any idea?
EDITED
I found an error on my database schema, now the error I get with
.Where("it.Group.IdGroup in {1,2,3,4,5}")
Is
'IdGroup' is not a member of 'Transient.collection[myModel.Group(Nullable=True,DefaultValue=)]'. To extract a property of a collection element, use a subquery to iterate over the collection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试一下:
linq:
为 In 创建扩展方法:
You can try this out:
linq:
Create an extension method for In:
LINQPad 代码似乎根据问题给出了预期结果
(架构名称:'master'、'state'、'group' 由 LINQPad 本身复数)
LINQPad code which seems to give expected result according to question
(schema names: 'master','state','group' pluralized by LINQPad itself)