Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将此Linq查询语法转换为方法语法?_C#_.net_Linq_Entity Framework - Fatal编程技术网

C# 如何将此Linq查询语法转换为方法语法?

C# 如何将此Linq查询语法转换为方法语法?,c#,.net,linq,entity-framework,C#,.net,Linq,Entity Framework,该查询包含以下查询: 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 = 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, (role, userRoles) => new {role, userRoles})
            .Join(_db.Users, @t => userRoles.UserId, user => user.Id, (@t, 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

第一步:使用。我想它可以复制给你。我可以为你做。你不需要在SelectMany上使用匿名类型的resultSelector,因为你只使用其中的用户角色。我缺少什么吗?当然,这是对查询的直译,但第三行的意思是什么?编辑:角色是否真的有一个名为Users的导航属性,而该属性不是由用户组成的?正如我从初始查询(请参阅“on userRoles.UserId等于user.Id”部分)中了解到的,该角色具有UserRole{UserId,RoleId}对象的集合。@ChrisV另外,它不是直译。里基的回答中有直译。@ChrisV最初的问题是关于AspNet身份的。见资料来源:和
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