查询组合

时间:2018-12-20 07:17:37

标签: entity-framework .net-core entity-framework-core

我正在尝试使用asp.net核心c#构建查询

https://www.reflectionit.nl/blog/2017/paging-in-asp-net-core-mvc-and-entityframework-core

我尝试进行过滤,但是我需要另一个具有唯一ID的表中的数据

var result = _context.UserRoles.Where(y => y.RoleId.Contains(selectedRoles.Id)); // Retrieve the the userid i have from another table with the selected roleid

var query = _context.Users.Where(x => //I have already tried contains, where join );

如果有一个我可以学习该查询的网站,请推荐。 “ Join()”在我进行分页时不起作用

1 个答案:

答案 0 :(得分:0)

至少两个解决方案(请注意,我不检查身份类成员,因此以下是解决方案的“精神”(您错过了select 子句)):

var result = _context.UserRoles.
    Where(y => selectedRoles.Contains(y.RoleId)).
    Select(y => y.User);

var result = _context.UserRoles.
     Where(y => selectedRoles.Contains(y.RoleId)).
     Select(y => y.UserId); 
query = _context.Users.
    Where(x => result.Contains(x.Id)); 

也就是说,假设Identity(v2)中没有公开UserRoles表,您可能想要:

userManager.Users.
    Where(u => u.Roles.Any(r => selectecRoles.Contains(r.RoleId)));

由您实例化userManager。