Find delta of 2 IEnumerable<T>

本文关键字:lt gt IEnumerable delta of Find | 更新日期: 2023-09-27 18:11:57

我从活动目录中获取用户和组,但是经过一些测试,我们发现memberOFmember不一致。例如,userA是groupW的成员,但groupW没有将userA列为成员。为了解决这个问题,我们必须让成员和成员同时同步它们。

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

我怎样才能得到?

我应该有两个字典或查找等

  1. allGroups列出在"用户"中未找到的所有组。MemberOF字典应该将键作为用户的组名和值列表。

示例groupA用户{A,B,C}, GroupB用户{A,C}

  1. 的权限列出在Group.member中未找到的所有用户字典应该有键作为用户名和组的值列表。

示例userA组{x,y}, userB组{x,z}

编辑:另一个例子:假设这是从Active directory返回的2个IEnumerable。

 `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"所在的所有群组时,我们可以看到"GroupA"被列出。"Mike"也一样,他是GroupB和GroupC的成员。GroupC未被列出。"大卫"在这种情况下,有正确的值。

还注意到,"Tim"被列在组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"}

Find delta of 2 IEnumerable<T>

我不完全确定这是否是你想要的…我明天会根据你对我评论的回应来改变我的答案。:)

假设你想要一个包含不属于该组的用户列表的组字典,以及一个包含不属于该组的用户列表的用户字典,那么这里是它的代码…

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