【问题标题】:How to assign the LINQ query values to a another dictionary如何将 LINQ 查询值分配给另一个字典
【发布时间】:2011-02-03 03:28:06
【问题描述】:

如何将 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;

    }

【问题讨论】:

  • 虽然我不知道这些标签与问题有什么关系。
  • @Yuriy - 我在其中编辑了这些标签。它绝对是 C#,他(我假设)试图从该集合/LINQ 查询中创建一个Dictionary&lt;T,TKey&gt;。这些标签不相关吗?
  • 抱歉,目前还不清楚...Dictionary&lt;string, Privilege&gt; 中的字符串是什么?角色名?这会将 1 个角色与 1 个权限相关联。如果每个角色拥有多个权限,那么您需要的是 Dictionary&lt;string, IEnumerable&lt;Privilege&gt;&gt;...
  • 实际上我的目标是我想比较两个列表中哪些值是常见的,我应该将它加载到字典中......
  • 为什么不能按照这里的建议使用 ToDictionary?

标签: c# linq generics dictionary


【解决方案1】:

实际上,您的代码并没有说明您希望放入字典的内容。什么应该是关键?与 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);

已编辑

好的,让我们想象一下两个字典的内容:

  • 角色 dic = [{"aaa", RolePrivilege1}, {"bbb", RolePrivilege2}, {"ccc", RolePrivilege3}]
  • 权限 dic = [{"aaa", Privilege5}, {"bbb", Privilege6}, {"ddd", Privilege7}]

您期望输出什么? 我想你想得到下一个序列:

  • 结果 = [{"aaa", Privilege5}, {"bbb", Privilege6}]

对吗?如果是,此请求将有所帮助:

        var Result = Role
            .Join(Privilege,
                outer => outer.Key,
                inner => inner.Key,
                (outer, inner) => new { Str = outer.Key, Privilege = inner.Value })
            .ToDictionary(_ => _.Str, _ => _.Privilege);

【讨论】:

  • 它只是一个 dic 我应该绑定它.......Dictionary 结果就是这样......
  • 或者是否有任何选项可以获取这两个 dic 角色 Privilege 中的共同值。并将其绑定到 Dictionary 中的另一个 dic ....
【解决方案2】:

你可能正在寻找Enumerable.ToDictionary(),但我只能猜到你想要做什么......

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 上而不是名称上进行连接(如果可能的话)。

也许这个问题会有所帮助:
Linq-to-SQL ToDictionary()

【讨论】:

  • 如何分配给这个 dic Dictionary 因为我想返回一个 s a dic..
猜你喜欢
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
相关资源
最近更新 更多