C# 查找2个IEnumerable的增量<;T>;

C# 查找2个IEnumerable的增量<;T>;,c#,.net-3.5,C#,.net 3.5,我正在从active directory获取用户和组,但是经过一些测试,我们发现memberOF与member不一致。示例userA是groupW的成员,但groupW没有将userA列为成员。为了解决这个问题,我们必须获得的成员和成员,然后同步他们 public class User { public string UserName { get; set; } public IList<string> MemberOf { get; set; }

我正在从active directory获取用户和组,但是经过一些测试,我们发现
memberOF
member
不一致。示例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
}

我希望它更清楚

我不完全确定这是否是你要问的。。。明天我将根据您对我的评论的回复来更改我的答案。:)

假设您需要一个包含不在该组中的用户列表的组字典,以及一个包含不在该组中的组列表的用户字典,那么下面是它的代码

    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 groupsWithUsersNotInThem=newdictionary();
var usersWithGroupsTheyArentIn=new Dictionary();
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);
    });
var missingGroups=new Dictionary();
var missinguers=新字典();
allUsers.ForEach(u=>
{
//获取组存在但此用户不在其中的列表
var groupsThisUserIsntIn=所有组
其中(g=>u.MemberOf.Contains(g.Name)和&!g.Members.Contains(u.UserName))
.Select(g=>g.Name).ToList();
//添加此用户所属但不在所有组中的组
groupsThisUserIsntIn.AddRange(u.MemberOf.Where(userGroupName=>allGroups.All(g=>g.Name!=userGroupName));
if(groupsThisUserIsntIn.Count()>0)
missinguers.Add(u.UserName,groupsThisUserIsntIn);
});
allGroups.ForEach(g=>
{
//获取用户存在但该组不在其中的列表
var usersNotInThisGroup=alluser
其中(u=>g.Members.Contains(u.UserName)和&!u.MemberOf.Contains(g.Name))
.Select(u=>u.UserName).ToList();
//加入该组织声称拥有但不在诱惑范围内的用户
usersNotInThisGroup.AddRange(g.Members.Where(groupUserName=>allUsers.All(u=>u.UserName!=groupUserName));
if(usersNotInThisGroup.Count()>0)
添加(g.Name,usersNotInThisGroup);
});

在第一个示例中,您是否试图查找不在groupA中的用户列表?例如,A、B和C不在groupA中,但所有其他用户都在groupA中?好的,我将编辑帖子,并尝试使其更清晰。我明白了。。。我不期望LDAP会给出这样不一致的结果,所以这就是我不期望的原因。容易修复。。。我会更新我的答案。谢谢你,凯文,不过我似乎没有解释清楚。不过,你的解决方案在另一个问题上帮助了我,谢谢。
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"}
    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);
    });