Entity Framework 4 Linq 帮助 - 从过滤的多个表中提取数据
不确定这是如何完成的,我设置了 .edmx,以便导航属性与表上的外键关系匹配。不确定我是否仍然需要执行联接,或者 EF 是否会让我通过导航属性自动访问相关表数据。
我需要做什么,根据 ContentView 获取所有 ContentSections 及其关联的 ContentItems 并按DiversionProgram.CrimeNumber。
我想取回 IEnumerable,对于每个 ContentSection,它应该可以通过导航属性 ContentItems 访问它的 ContentItems
谢谢
Not sure how this is done, I have my .edmx set up so that the navigation properties match the foreign key relationships on the tables. Not sure if I still need to perform joins or if EF will give me access to the related table data through the navigational properties automatically.
What I need to do it get all the ContentSections and their associated ContentItems based on the ContentView and filtered by the DiversionProgram.CrimeNumber.
I would like to get back IEnumerable, for each ContentSection it should have access to it's ContentItems via the navigation property ContentItems
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
比如:
我已经解释过了
作为
cs.ContentView.ContentViewID == someID
这将为您提供给定 ContentView 的所有 ContentSections。并解读
为
cs.ContentItems.Where(ci => ci.DiversionProgram.CrimeNumber == someCrimeNumber)
,这将为您提供所有具有特定 CrimeNumber 的 ContentItem。
或者你的意思是基于/过滤的其他东西。也许是 OrderBy,或者所有那些 ContentSections,其中任何一个 ContentItems 都会有一个特定的 CrimeNumber?
Something like:
I've interpreted
as
cs.ContentView.ContentViewID == someID
This will give you all the ContentSections for a given ContentView. And interpreted
as
cs.ContentItems.Where<ContentItem>(ci => ci.DiversionProgram.CrimeNumber == someCrimeNumber)
which will give you all those ContentItems that have a specific CrimeNumber.
Or did you mean something else with based on / filtered by. Maybe OrderBy, or all those ContentSections where Any of it's ContentItems would have a certain CrimeNumber?
您可以急切地加载以获取所有关联记录,但是当您想要开始过滤/排序时,不必担心
包含
。只需使用匿名类型进行投影,EF 就会计算出它需要做什么。它有点毛茸茸的,但它会起作用。如果它变得太复杂,那就硬着头皮使用 SPROC。
现在,带着这个警告,类似这样的事情(从我的脑海中浮现出来):
You can eager load to get all associated records, but when you want to start filtering/ordering, don't bother with
Include
.Just do a projection with anonymous types and EF will work out what it needs to do. It's a bit hairy, but it'll work. If it get's too complicated, bite the bullet and use a SPROC.
Now, with that caveat, something like this (off the top of my head):
如果您使用 CTP5,您可以做一些非常独特的事情,如下所示:
您可以了解有关 CTP5 的更多信息以及如何在数据库优先场景中使用它 此处
If you use the CTP5 you can do something very unique it looks like this:
You can learn more about the CTP5 and how it can be used in Database first scenario here