linq包括内在加入ISN´ t工作

发布于 2025-02-02 23:58:52 字数 445 浏览 3 评论 0原文

我一直在尝试运行查询,

dbContext.Model.Include(x => x.ListItems);

但是我总是得到一个左联接,我需要一个内在的加入。我尝试将其添加到我的上下文类中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LogTL>()
                        .HasOne(p => p.ListaFluxos)
                        .WithMany()
                        .IsRequired(); ;
        }

但是仍然没有成功。

I´ve been trying to run the query

dbContext.Model.Include(x => x.ListItems);

But I always get a Left Join and i need a Inner Join. I tried adding this to my context class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LogTL>()
                        .HasOne(p => p.ListaFluxos)
                        .WithMany()
                        .IsRequired(); ;
        }

but still no success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

清风疏影 2025-02-09 23:58:53

为什么需要内部加入? EF默认情况下将使用左联接来容纳可能没有ListItem的模型。

如果您只需要具有ListItems的模型,则可以使用以下方式查询:

dbContext.Model.Include(x => x.ListItems).Where(x => x.ListItems.Any()).ToList();

尽管这可能不会导致EF产生内部联接。最后,我想重要的是为什么您觉得您需要内部连接,而与左连接在一起。如果关注性能,通常可以通过解决索引和利用诸如投影之类的内容来优化查询运行之类的内容来满足。相反,如果您想通过EF复制一个非常具体的查询(通常更复杂),则更好的方法可以是利用数据库中的视图之类的东西,并将实体模型映射到结果。

Why do you need an Inner Join? EF by default will use a Left Join to accommodate Models that may not have ListItems.

If you want only Models that have ListItems then you can query that with:

dbContext.Model.Include(x => x.ListItems).Where(x => x.ListItems.Any()).ToList();

Though that may not result in EF producing an Inner Join. In the end I guess what matters is why you feel you need an Inner Join vs. a Left Join. If it is a concern around performance, that is generally better met by addressing indexing and leveraging things like projection to optimize the queries run. If instead there is a very specific (and typically more complex) query you want to reproduce via EF, a better approach can be to leverage something like a View in the database and map an entity model(s) to the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文