C# 如何将LINQ查询值分配给另一个字典

C# 如何将LINQ查询值分配给另一个字典,c#,linq,generics,dictionary,C#,Linq,Generics,Dictionary,如何将Linq查询的结果分配给字典 public Dictionary<string, Privilege> GetAllPermissionsByGroupId(string groupid) { string[] Roles = new string[] { "e8b08a45-9cb5-4ac9-8c6c-9dfe4ac23966$Moderator" }; List<RolePrivilege> RoleList =

如何将Linq查询的结果分配给字典

 public Dictionary<string, Privilege> GetAllPermissionsByGroupId(string groupid)
    {

        string[] Roles = new string[] { "e8b08a45-9cb5-4ac9-8c6c-9dfe4ac23966$Moderator" };

        List<RolePrivilege> RoleList = new List<RolePrivilege>();
        List<Privilege> PrivilegeList = new List<Privilege>();

        Dictionary<string, RolePrivilege> Role = PrivilegeProxy.GetPrivilegesForRoles("744A2BE3-846E-4E4A-9796-DAF9C743E8FF", Roles);
        RoleList = Role.Values.ToList();


        Dictionary<string, Privilege> Privilege = PrivilegeProxy.GetPrivilegesbyModuleIds(new string[] { "Groups" });
        PrivilegeList = Privilege.Values.ToList();

        var identicalQuery = from roles in RoleList
                            join privileges in PrivilegeList on roles.PrivilegeName equals privileges.Name
                           select new { Roles = roles, Privileges = privileges };


        Dictionary<string, Privilege> Result=new Dictionary<string,Privilege>();
         Result=?


         return Result;

    }

你可能在找,但我只能猜测你想做什么

var query = from employee in employees
            join privilege in privileges
              on employee.PrivilegeName equals privilege.Name
            select new
            {
                Employee = employee,
                Privilege = privilege
            };

var dictionary = query.ToDictionary(o => o.Employee);
由于employeeA的类型为RolePrivilege,而employeeB的类型为Privilege,因此您的变量令人困惑。此外,如果可能的话,最好在ID上而不是在名称上进行连接

也许这个问题会有帮助:

您可能正在寻找,但我只能猜测您想做什么

var query = from employee in employees
            join privilege in privileges
              on employee.PrivilegeName equals privilege.Name
            select new
            {
                Employee = employee,
                Privilege = privilege
            };

var dictionary = query.ToDictionary(o => o.Employee);
由于employeeA的类型为RolePrivilege,而employeeB的类型为Privilege,因此您的变量令人困惑。此外,如果可能的话,最好在ID上而不是在名称上进行连接

也许这个问题会有帮助:

实际上,您的代码并没有说明您希望在字典中输入的内容。关键是什么?与RolePrivilege关联的字符串

不管怎样,我建议您在字典中使用对而不是值:

var Roles = new string[] { "e8b08a45-9cb5-4ac9-8c6c-9dfe4ac23966$Moderator" };
Dictionary<string, RolePrivilege> Role = PrivilegeProxy.GetPrivilegesForRoles("744A2BE3-846E-4E4A-9796-DAF9C743E8FF", Roles);
Dictionary<string, Privilege> Privilege = PrivilegeProxy.GetPrivilegesbyModuleIds(new string[] { "Groups" });

var identicalQuery = from roles in Role
                     join privileges in Privilege on roles.Value.PrivilegeName equals privileges.Value.Name
                     select new { Roles = roles, Privileges = privileges };

Dictionary<string, Privilege> Result = identicalQuery.ToDictionary(_ => _.Roles.Key, _.Privileges.Value);

实际上,您的代码并没有说明您希望将什么放入字典中。关键是什么?与RolePrivilege关联的字符串

不管怎样,我建议您在字典中使用对而不是值:

var Roles = new string[] { "e8b08a45-9cb5-4ac9-8c6c-9dfe4ac23966$Moderator" };
Dictionary<string, RolePrivilege> Role = PrivilegeProxy.GetPrivilegesForRoles("744A2BE3-846E-4E4A-9796-DAF9C743E8FF", Roles);
Dictionary<string, Privilege> Privilege = PrivilegeProxy.GetPrivilegesbyModuleIds(new string[] { "Groups" });

var identicalQuery = from roles in Role
                     join privileges in Privilege on roles.Value.PrivilegeName equals privileges.Value.Name
                     select new { Roles = roles, Privileges = privileges };

Dictionary<string, Privilege> Result = identicalQuery.ToDictionary(_ => _.Roles.Key, _.Privileges.Value);

虽然我不知道这些标签和这个问题有什么关系。@Yuriy-我在中编辑了这些标签。它肯定是C,我想他正试图从集合/LINQ查询创建一个字典。那些标签不相关吗?抱歉,但还是不清楚。。。字典里的字符串是什么?角色名?将1个角色与1个权限关联。如果每个角色有多个权限,那么您需要的是一个字典……实际上,我的目标是比较两个列表中哪些值是常见的,我应该将其加载到字典中……为什么您不能按照这里的建议使用ToDictionary?尽管我不知道这些标记与问题有什么关系。@Yuriy-我编辑了这些把标签放进去。它肯定是C,我想他正试图从集合/LINQ查询创建一个字典。那些标签不相关吗?抱歉,但还是不清楚。。。字典里的字符串是什么?角色名?将1个角色与1个权限关联。如果每个角色有多个权限,那么您需要的是一个字典…实际上,我的目标是比较两个列表中哪些值是常见的,我应该将其加载到字典中…为什么您不能按照这里的建议使用ToDictionary?如何分配此dic字典,因为我想返回一个s a dic..如何分配此项dic Dictionary因为我想返回一个s a dic..它只是一个dic我应该绑定它..Dictionary Result就是这样..或者在这两个dic角色中有没有获得公共值的选项,特权。并将其绑定到字典中的另一个dic…它只是一个dic我应该绑定它…字典结果就是这样…或者在这两个dic角色中是否有获得公共值的选项,特权。并将其绑定到字典中的另一个dic。。。。