I need some help on a sql query, for which I am using EntityFramework as ORM.
I have a User-- Group Many to Many relationship, i.e one user can belong to more than one group and one group can have more than 1 user.
The way I have done the mapping is..
USER tbl, Usr_Grp table, and Group Table where PK = Primary Key, FK = Foreign Key
USER table has --> UserId(PK) , UserName
Group table has --> GroupId (PK), GroupName Usr_Grp table has --> Id(PK), UUserId(FK to usertable), GGroupId (FK to GroupTable)The issue that I am facing is... In my api , I will get a set of GroupId's and I want to find only those Users that belong to all these GroupId's ( i.e all the groups th开发者_JAVA百科at are passes in).
Is there a way to write this query in entity framework or Sql. I would really appreciate any help I can get on this.
-RN
Junction table Usr_Grp
shouldn't have Id. If you create complex primary key over (UUserId, GGroupId), then EF automatically would understand, that it is Many-To-Many relation.
If you make your relations as follows:
Then EF will generate entities with Many-To-Many relations:
After you have everything set up you can easily use such code:
var ids = new List<int>{1, 2, 3, 4}; // GroupIds you need
context.Groups
.Where(x=> ids.Contains(x.Id))
.SelectMany(x=>x.Users)
.Distinct()
.ToArray();
If you cannot change model, then just use such linq query:
context.Grp_Usrs
.Where(x=> ids.Contains(x.GroupId))
.SelectMany(x=>x.Users)
.Distinct()
.ToArray();
Try this method:
var gid = new List<int> {1, 2, 3, 4};
var users_id = entity.Usr_Grp.where(x=> gid.Contains(x.uid);
Hope it helps!
//Here is a LinqToSql example but it should work with EF too.Taking into consideration your mappings I would have the following approach in handling a many to many relationship:
//1. First You need to save you records into the tables as many to many relationship
[Test]
public void CanSaveGroupUsers()
{
var group = Group { CreatedDate = DateTime.UtcNow, Name = "The Rookies" };
var groupId = _groupRepository.SaveGroup(group);
var user = new User { CreatedDate = DateTime.Now, Name = "Justin Bieber" };
var userId = _userRepository.SaveUser(user);
var userGroup = new UserGroup { GroupId = groupId, UserId = userId };
_group.SaveUserGroup(userGroup);
}
//2. Then you can retrive them this way:
[Test]
public void CanGetGroupUsers()
{
var groupIds = new List<int>{1,2,3,4};
var users = _usersRepository.GetAllUsers();
var specificUsersList = (users.AsQueryable().Where(user => groupIds.Contains(user.UserGroups.FirstOrDefault().GroupId)));
}
//3. I attached my repository code just in case
public IQueryable<User> GetAllUsers()
{
_db.ObjectTrackingEnabled = false;
return _db.Users;
}
public int SaveGroup(Group Group)
{
var dbGroup = new Group
{
Name = Group.Name,
CreatedDate = Group.CreatedDate
};
_db.Groups.InsertOnSubmit(dbGroup);
_db.SubmitChanges();
return dbGroup.Id;
}
public int SavUser(User user)
{
var dbUser = new User
{
CreatedDate = user.CreatedDate,
Name = user.Name
};
_db.Users.InsertOnSubmit(dbUser);
_db.SubmitChanges();
return dbUser.UserId;
}
public void SaveUserGroup(UserGroup userGroup)
{
var dbUserGroup = new UserGroup
{
GroupId = userGroup.GroupId,
UserId = userGroup.UserId
};
_db.UserGroups.InsertOnSubmit(dbUserGroup);
_db.SubmitChanges();
}
//Hope this helps:)
精彩评论