Entity Framework 4 Linq 帮助 - 从过滤的多个表中提取数据

发布于 2024-10-16 09:46:09 字数 358 浏览 2 评论 0原文

不确定这是如何完成的,我设置了 .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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分分钟 2024-10-23 09:46:09

比如:

using(Entities context = new Entities())
{
  IEnumerable<ContentSection> enumerator = context.ContentSections
  .Include("ContentItems")
  .Where<ContentSection>(cs => cs.ContentView.ContentViewID == someID && cs.ContentItems.Where<ContentItem>(ci => ci.DiversionProgram.CrimeNumber == someCrimeNumber))
  .AsEnumerable<ContentSection>
}

我已经解释过了

基于ContentView

作为 cs.ContentView.ContentViewID == someID

这将为您提供给定 ContentView 的所有 ContentSections。并解读

按 DiversionProgram.CrimeNumber 过滤

cs.ContentItems.Where(ci => ci.DiversionProgram.CrimeNumber == someCrimeNumber)

这将为您提供所有具有特定 CrimeNumber 的 ContentItem。

或者你的意思是基于/过滤的其他东西。也许是 OrderBy,或者所有那些 ContentSections,其中任何一个 ContentItems 都会有一个特定的 CrimeNumber?

Something like:

using(Entities context = new Entities())
{
  IEnumerable<ContentSection> enumerator = context.ContentSections
  .Include("ContentItems")
  .Where<ContentSection>(cs => cs.ContentView.ContentViewID == someID && cs.ContentItems.Where<ContentItem>(ci => ci.DiversionProgram.CrimeNumber == someCrimeNumber))
  .AsEnumerable<ContentSection>
}

I've interpreted

based on the ContentView

as cs.ContentView.ContentViewID == someID

This will give you all the ContentSections for a given ContentView. And interpreted

filtered by the DiversionProgram.CrimeNumber

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?

毁虫ゝ 2024-10-23 09:46:09

您可以急切地加载以获取所有关联记录,但是当您想要开始过滤/排序时,不必担心包含

只需使用匿名类型进行投影,EF 就会计算出它需要做什么。它有点毛茸茸的,但它会起作用。如果它变得太复杂,那就硬着头皮使用 SPROC。

现在,带着这个警告,类似这样的事情(从我的脑海中浮现出来):

var query = ctx.ContentView
               .Select(x => new 
{
   ContentSections = x.ContentSections
                      .Where(y => y.ContentItems
                        .Any(z => z.DivisionProgram.CrimeNumber = 87))
}).ToList().Select(x => x.ContentSections);

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):

var query = ctx.ContentView
               .Select(x => new 
{
   ContentSections = x.ContentSections
                      .Where(y => y.ContentItems
                        .Any(z => z.DivisionProgram.CrimeNumber = 87))
}).ToList().Select(x => x.ContentSections);
断桥再见 2024-10-23 09:46:09

如果您使用 CTP5,您可以做一些非常独特的事情,如下所示:

var context = new YourEntitiesContext();

var query = context.ContentView.Include(cs => cs.ContentSections
                   .Select(ci => ci.ContentItems
                   .Select(dp => dp.DiversionProgram)
                   .Where(dp.CrimeNumber == crimeNumber)))
                   .Where(cv => cv.ContentViewID == contentViewID).FirtsOrDefault();

您可以了解有关 CTP5 的更多信息以及如何在数据库优先场景中使用它 此处

If you use the CTP5 you can do something very unique it looks like this:

var context = new YourEntitiesContext();

var query = context.ContentView.Include(cs => cs.ContentSections
                   .Select(ci => ci.ContentItems
                   .Select(dp => dp.DiversionProgram)
                   .Where(dp.CrimeNumber == crimeNumber)))
                   .Where(cv => cv.ContentViewID == contentViewID).FirtsOrDefault();

You can learn more about the CTP5 and how it can be used in Database first scenario here

慈悲佛祖 2024-10-23 09:46:09
 var query = from t1 in studentManagementEntities.StudentRegistrations
                        join t2 in studentManagementEntities.StudentMarks
                        on t1.StudentID equals t2.StudentID
                        select new
                        {
                            t1.selected column name,
                            t2.selected column name
                        };
 var query = from t1 in studentManagementEntities.StudentRegistrations
                        join t2 in studentManagementEntities.StudentMarks
                        on t1.StudentID equals t2.StudentID
                        select new
                        {
                            t1.selected column name,
                            t2.selected column name
                        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文