简体   繁体   中英

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. I have defined my entities classes with foreign keys and navigation(entities) properties. I would rather lambda expressions methods.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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