我正在使用实体框架6。在我的数据库上,我有这些表:
MasterTable ( Id , name)
Child1 ( ID , name , vl1 , Master_ID)
Child2 ( ID , name , vl2 , MasterID )
Child3 (ID , name , vl3 , Master_ID )
Child3Itm ( ID , name , Child3_ID)
对于给定的MasterTable
项目,我想从数据库中加载一个查询:
- 所有
Child1
其中vl1 > 5
- 所有
Child2
其中vl2 > 6
- 所有
Child3
其中vl3 > 7
和每个Child3
中加载所有Child3Itm
内容。
我正在使用此查询:
Dim lst = (From t In context.MasterTable.Where(Function(t1) t1.id = 7)
Select New With {
t,
.chld1 = t.child1s.Where(Function(t21) t21.vl1 >5),
.chld2 = t.child2s.Where(Function(t31) t31.vl2>6 ),
.chld3 = t.child3s.Where(Function(t41) t41.vl3>7).Select(Function(t411) t411.Child3Itms)
}).ToList
问题是未选择Child3
。所有其他都可以。我能做些什么?预先感谢!
而无需看到您的数据,很难诊断。但是,由于您可以访问调试器,因此可以自己做。在下面的代码下,越过每行并检查变量。应该很容易看到您的代码失败的位置
Dim masters = From t In context.MasterTable Where t = 7
Dim child1s = From m In masters Where m.child1s.vl1 > 5
Dim child2s = From m In masters Where m.child2s.vl2 > 6
Dim child3s = From m In masters Where m.child3s.vl3 > 7
Dim child3i = child3s.Child3Itms
可能是您问题的错别字。确保Child3S的状况正确:
t41.vl>7
应该是吗?
t41.vl3 > 7
Dim lst = (From t In context.MasterTable.Where(Function(t1) t1.id = 7)
Select New With {
t,
.chld1 = t.child1s.Where(Function(t21) t21.vl1 >5),
.chld2 = t.child2s.Where(Function(t31) t31.vl2>6 ),
.chld3 = t.child3s.Where(Function(t41) t41.vl3>7),
.chld3itms = t.child3s.Where(Function(t51) t51.vl3>7).Select(Function(t511) t511.Child3Itms)
}).ToList
Dim lst = (From t In context.MasterTable.Where(Function(t1) t1.id = 7)
Select New With {
t,
.chld1 = t.child1s.Where(Function(t21) t21.vl1 >5),
.chld2 = t.child2s.Where(Function(t31) t31.vl2>6 ),
.chld3 = (From t2 in t.child3s.Where(Function(t41) t41.vl3>7)
Select New With {
t2,
.chld3it=t2.Child3Itms
})
}).ToList
这也可以正常工作。如果有人可以告诉我这个答案是更好还是否,则与Antoni发布的其他解决方案相比?