简体   繁体   English

对特定关系模型中具有EF的实体的Linq

[英]Linq to entities with EF in an specific relational model

If someone could give me an idea with it, it would really appreciate it. 如果有人可以给我一个主意,那就真的很感激。 How can I get all Users (not related entities) that have records in the Connections Table and belong to a Company (Table) with an id predefined (let's say CompanyId=1), using Entity framework and linq to entities. 我如何使用实体框架和对实体的linq来获取所有在连接表中具有记录并且属于公司(表)且其ID预定义(例如CompanyId = 1)的用户(非相关实体)。 I have defined my entities classes with foreign keys and navigation(entities) properties. 我已经使用外键和Navigation(entities)属性定义了实体类。 I would rather lambda expressions methods. 我宁愿使用lambda表达式方法。

This is the relational model: 这是关系模型:

在此处输入图片说明

Do you mean sth like this? 你的意思是这样吗?

var users = dbContext.Users.
             Where(usr => usr.UserTeams.Any(
                                   usrTeam => usrTeam.Team.CompanyId == 1)) 
             Where(usr => usr.Connections.Any());

So if you defined your foreign key constraints and your navigational properties correctly this should work: 因此,如果您正确定义了外键约束和导航属性,则此方法应该起作用:

                var users = db.Companies.Include("TeamId")
                                        .Include("UserId")
                                        .Include("ConnectionId")
                                        .Select(x=>x.Teams.Users.Username)
.where(x=>x.Teams.Users.Connections!=null && x.CompanyId==1).tolist();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM