c# - Entity Framework 子选择

标签 c# entity-framework linq-to-entities

我正在尝试根据与公司匹配的用户名返回结果。涉及5张表。我可以做一个子选择(显然不是)或者最好的方法是什么。

db.Displays
     .Include(d => d.Location)
     .Include(d => d.Location.Region)
     .Include(d => d.Location.Region.Company.Users.Where(p=> p.LoginName == userName);

最佳答案

您不需要应用前两个Includes,第三个您应该能够包含您需要的级别:

var displays= db.Displays.Include(d=>d.Location.Region.Company.Users).Where(d=>d.Location.Region.Company.Users.Any(p=>p.LoginName== userName));

Include之后,您需要调用Where方法来按您要应用的条件过滤显示。

检查这个link看看 Include 是如何工作的扩展方法。

关于c# - Entity Framework 子选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29611441/

相关文章:

c# - Linq to Entities 奇怪的部署行为

c# - 如何在 Visual Studio 中读取内存快照

entity-framework - 使用 Code First 迁移的 Entity Framework 的唯一约束

C# EF Code first - 创建数据库和添加数据

sql-server - 优化 Entity Framework 生成的 SQL Server 执行计划

c# - 不支持 Linq to Entities SqlFunctions.DateDiff

c# - 使用 MVP-VM 设计模式在 Presenter 中正确使用模型

c# - MVC - 将附加信息传递给 View

c#正则表达式问题

entity-framework - 即使在表对象中,EF 也不添加对象