1

How to assign the result of the Linq Query to a dictionary...

 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;

    }
Kajah User
  • 593
  • 5
  • 17
  • 56
  • Although I have no idea what these tags have to do with the question. – Yuriy Faktorovich Feb 03 '11 at 03:33
  • @Yuriy - i edited those tags in. It's definetely C#, and he's (i assume) attempting to create a `Dictionary` from that collection/LINQ query. Are those tags not relevant? – RPM1984 Feb 03 '11 at 03:36
  • Sorry, but it's still not clear... what's the string in `Dictionary`? The role name? That would associate 1 role to 1 Privilege. If you have more than one privilege per role then what you need is a `Dictionary>`... – Xavier Poinas Feb 03 '11 at 06:04
  • actually my goal is i want to compare the two lists in that which which values are common i should load it in the dictionary... – Kajah User Feb 03 '11 at 06:09
  • Why cant you use ToDictionary as suggested here? – Magnus Feb 03 '11 at 14:09

2 Answers2

1

Actually you code does not illustrate what you desire to put into dictionary. What should be key? String associated with RolePrivilege?

Nevermind, I would recommend work with pairs instead of values in your dictionaries:

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);

EDITED

Okay, let's imagine contents of both dictionaries:

  • Role dic = [{"aaa", RolePrivilege1}, {"bbb", RolePrivilege2}, {"ccc", RolePrivilege3}]
  • Privilege dic = [{"aaa", Privilege5}, {"bbb", Privilege6}, {"ddd", Privilege7}]

What do you expect as output? I suppose you want to get next sequense:

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

Correct? If yes, this request will help:

        var Result = Role
            .Join(Privilege,
                outer => outer.Key,
                inner => inner.Key,
                (outer, inner) => new { Str = outer.Key, Privilege = inner.Value })
            .ToDictionary(_ => _.Str, _ => _.Privilege);
Alex Zhevzhik
  • 3,347
  • 19
  • 19
0

You are probably looking for Enumerable.ToDictionary(), but I can only guess what you're trying to do...

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);

Your variables are confusing because employeeA is of type RolePrivilege and employeeB is of type Privilege. Also, it would be better to do the join on an ID instead of a name (if possible).

Maybe this question will help:
Linq-to-SQL ToDictionary()

Community
  • 1
  • 1
Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95