answer here包含以下查询:
var query = from role in _db.Roles
where role.Name == roleName
from userRoles in role.Users
join user in _db.Users
on userRoles.UserId equals user.Id
select user;
如何使用 Linq 方法语法重现相同的查询?
最佳答案
var query = _db.Roles
.Where(role => role.Name == roleName)
.SelectMany(role => role.Users)
.Join(_db.Users, userRole => userRole.UserId, user => user.Id, (role, user) => user);
一些解释
var query = from role in _db.Roles
where role.Name == roleName // this will be translated to .Where
from userRoles in role.Users // this is .SelectMany
join user in _db.Users // this is .Join
on userRoles.UserId equals user.Id // second and third arguments of .Join
select user; // last argument of .Join
关于c# - 如何将此 Linq 查询语法转换为方法语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31127198/