2013-05-01 50 views
0

我从活动目录获取用户和组,但经过一些测试后,我们发现memberOF未与member对齐示例userA是groupW的成员,但groupW未列出userA作为成员。为了解决这个问题,我们必须让会员及会员然后同步他们。查找2的三角洲IEnumerable <T>

public class User 
    { 
     public string UserName { get; set; } 
     public IList<string> MemberOf { get; set; } // list of group names 
    } 

public class Group 
{ 
    public string Name { get; set; } 
    public IList<string> Members { get; set; } // list of username 
} 

从Active Directory中我得到

IEnumerable<Group> allGroups 
IEnumerable<User> allUsers 

我怎样才能获得增量?

我应该有2本字典或查找...等

  1. allGroups 列表User.MemberOF 未发现各组字典应具备的关键,因为用户的组名和值列表。

例如组A的用户{A,B,C},组B的用户{A,C}

  1. ALLUSERS 列表中的所有的用户不Group.member 词典中找到应具有键作为用户名和组的值列表。

例如用户A组{X,Y},用户B组{X,Z}

编辑:又如:让我们说,这是第2 IEnumerable的从Active Directory返回。

`IEnumerable<Group> allGroups` contains 
- GroupA {"Mike","Jan","David"} 
- GroupB {"Kim","David","Jolan","Tim"} 

    // where Groupx is the name of the group and between {""} is the list of member 

    IEnumerable<User> allUsers contains 

- Mike {"GroupA","GroupB","GroupC"} 
- David {"GroupA","GroupB"} 
- Jolan {"GroupB","GroupC"} 

在这个例子中,我们可以看到,当我们要求LDAP获得groupA的所有成员时,“Jolan”未被列出。但是,当我们要求得到所有群体,其中“Jolan”是成员,那么我们可以看到,“A组”中列出。同样的,“迈克”,他是B族中的一员,也C组。 GroupC未列出。 “大卫” 在这种情况下,有正确的价值观。

还要注意的是,“提姆”,在B组上市之后,虽然他不是在allUsers

结果应该是这样的

Dictionary<string,IList<string>> missingUsers; 
Item 1 > key="Mike", Value={"GroupB","GroupC"} 
Item 2 > Key="Jolan" , Value= {"GroupC"} 

Dictionary<string,IList<string>> missingGroup; 
item 1 > Key="GroupB",{"Tim"} 

我希望这是更清晰

+0

在您的第一个示例中,您是否尝试在groupA中查找** not **的用户列表? IE浏览器。 A,B和C在组A中不是**,但所有其他用户**在组A中都是**? – Kevin 2013-05-01 22:38:08

+0

好吧,我将编辑帖子,并尝试使其更清楚。 – Maro 2013-05-01 23:35:15

+0

我明白了...我并不期望LDAP会给出不一致的结果,所以这就是我没有想到的原因。易于修复...我会更新我的答案。 – Kevin 2013-05-02 15:41:26

回答

1

我不能完全肯定,如果这是你的要求或不是...我会根据你的回应我的评论明天改变我的答案。 :)

假设你想团与谁该组中没有用户列表的字典,以及用户与组列表的字典他们不是那么这里就是它的代码...

var groupsWithUsersNotInThem = new Dictionary<Group, List<User>>(); 
    var usersWithGroupsTheyArentIn = new Dictionary<User, List<Group>>(); 
    allUsers.ForEach(u => 
     { 
      var groupsThisUserIsntIn = groups.Where(g => !g.Members.Contains(u.UserName)).ToList(); 
      if (groupsThisUserIsntIn.Count() > 0) 
       usersWithGroupsTheyArentIn.Add(u, groupsThisUserIsntIn); 
     }); 
    allGroups.ForEach(g => 
    { 
     var usersNotInThisGroup = users.Where(u => !u.MemberOf.Contains(g.Name)).ToList(); 
     if (usersNotInThisGroup.Count() > 0) 
      groupsWithUsersNotInThem.Add(g, usersNotInThisGroup); 
    }); 

编辑:让上面的代码的情况下,这是非常有用的

这是新的代码,解决实际问题三角洲...只找到用户/组,其中另一个列表不匹配。

var missingGroups = new Dictionary<String, List<String>>(); 
    var missingUsers = new Dictionary<String, List<String>>(); 
    allUsers.ForEach(u => 
    { 
     // get the list where the group exists but this user isn't in it 
     var groupsThisUserIsntIn = allGroups 
      .Where(g => u.MemberOf.Contains(g.Name) && !g.Members.Contains(u.UserName)) 
      .Select(g => g.Name).ToList(); 
     // add in the groups this user says he belongs to but that aren't in allGroups 
     groupsThisUserIsntIn.AddRange(u.MemberOf.Where(userGroupName => allGroups.All(g => g.Name != userGroupName))); 
     if (groupsThisUserIsntIn.Count() > 0) 
      missingUsers.Add(u.UserName, groupsThisUserIsntIn); 
    }); 
    allGroups.ForEach(g => 
    { 
     // get the list where the user exists but this group isn't in it 
     var usersNotInThisGroup = allUsers 
      .Where(u => g.Members.Contains(u.UserName) && !u.MemberOf.Contains(g.Name)) 
      .Select(u => u.UserName).ToList(); 
     // add in the users this group says it has but that aren't in allUsers 
     usersNotInThisGroup.AddRange(g.Members.Where(groupUserName => allUsers.All(u => u.UserName != groupUserName))); 
     if (usersNotInThisGroup.Count() > 0) 
      missingGroups.Add(g.Name, usersNotInThisGroup); 
    }); 
+0

谢谢凯文,不过看起来我没有明确解释它 但是,您的解决方案帮助我解决了另一个问题,谢谢。 – Maro 2013-05-02 13:26:00