I have a Lookup that has a master list of priority levels and groups. Also, a List that has the groups an user is in. ILookup<int, string>, List<string>. I’d like to compare the List to ILookup and return the IGroupings that match the groups in the List. Then finally get the IGrouping with the higher priority (ex: returns 6 instead 3).
My issue is with the actual comparision of the List and ILookup. It seems I might have to use a for-loop. But I think I just haven’t found the right Linq statement(s) yet.
What I’ve tried below.
List<string> groupsOfUser = whatGroupsUserBelongTo();
ILookup<int, string> loginGroups = mapADToRole.SelectMany(ad => ad.Value
.Select(grp => new {ad.Key, Value = grp}))
.ToLookup(adLkUp => adLkUp.Key, adLkUp => adLkUp.Value);
var blah = loginGroups.Where(adGrp => adGrp.SelectMany(ad => groupsOfUser.Contains(ad)));
UPDATE:
mapADToRole is Dictionary<int, List<string>>. The code below works but using foreach loops.
public static string getWindowsGroup()
{
List<string> groupsOfUser = whatGroupsUserBelongTo();
ILookup<int, string> loginGroups = mapADToRole.SelectMany(ad => ad.Value
.Select(grp => new {ad.Key, Value = grp}))
.ToLookup(adLkUp => adLkUp.Key, adLkUp => adLkUp.Value);
List<IGrouping<int, string>> foo = new List<IGrouping<int, string>>();
foreach (IGrouping<int, string> loginGroup in loginGroups)
{
foreach (var blah in loginGroup)
{
if (groupsOfUser.Contains(blah))
{
foo.Add(loginGroup);
}
}
}
var foobar = foo.Max(grp => grp.Key);
return loginGroups[foobar].FirstOrDefault();
}
I am not sure but this should do what your function does: