[英]Entity Framework Querying a Many to Many to Many relationship
I have a database where Users can belong to multiple Roles and Roles can have multiple Permissions. 我有一个数据库,其中用户可以属于多个角色,角色可以有多个权限。 Both relationships are many to many.
这两种关系都很多。 I want query and generate a list of the Rermissions a User has.
我想要查询并生成用户具有的重新发送列表。 I am trying to accomplish this by querying the Roles table to see what Roles the User is a member of and then I want to query and see what distinct Permissions each Role contains.
我试图通过查询Roles表来查看用户所属的角色,然后我想查询并查看每个角色包含的不同权限。 However I can't seem to get the LINQ correct.
但是我似乎无法让LINQ正确。
var permissions = RoleRepository.Get()
.Where(x => x.Users.Contains(user))
.Select(x => x.Permissions);
The above code gives me a list of list of Permissions, I just want a list of Permissions. 上面的代码给了我一个权限列表列表,我只想要一个权限列表。 Is there anyway (in LINQ) to take the union of all these lists?
无论如何(在LINQ中)采用所有这些列表的联合? Or is there a better way to accomplish this?
或者有更好的方法来实现这一目标吗?
Use SelectMany
Instead, SelectMany
flattens queries that return lists of lists 使用
SelectMany
相反, SelectMany
会展开返回列表列表的查询
So Try this : 试试这个:
var permissions = RoleRepository.Get().Where(x => x.Users.Contains(user))
.SelectMany(x => x.Permissions);
Hope this will help !! 希望这会有所帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.