How to assign the result of the Linq Query to a dictionary...
public Dict开发者_高级运维ionary<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;
}
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);
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()
精彩评论