0

以下代码填充不属于现有列表“selectedUsers”的用户列表。

foreach (var childOrg in viewModel.ChildOrganizations)
{
    List<OrganizationUserViewModel> childOrgUsers = new List<OrganizationUserViewModel>();
    var users = this._organizationManager.GetOrganizationUsers(childOrg.OrganizationId);
    foreach (var user in users)
    {
        var userViewModel = Gateway.Instance.Map<User, OrganizationUserViewModel>(user.User);
        userViewModel.Organization_UserId = user.Organization_UserId;
        if (selectedUsers != null)
        {
            foreach (OrganizationUserViewModel selectedUser in selectedUsers)
            {
                if (selectedUser.UserId != userViewModel.UserId)
                {
                    childOrgUsers.Add(userViewModel);
                }
            }
        }
    }
    childOrg.Users = childOrgUsers;
}

如果 'selectedUsers' 中只有一个用户,这可以正常工作,但如果有多个条目,则由于 foreach 循环,会在新列表中添加多个。我将如何避免这种情况?

提前感谢您的帮助。

编辑:我刚刚通读了我的问题,我可能应该澄清一下,因为我真的没有说清楚。当 foreach 为列表中的每个条目运行时,它还会在 ID 与 userViewModel 中的当前条目不匹配时为所选用户添加条目。这些是我真正需要重点删除的内容,因为我希望存储在 selectedUsers 中的任何内容都不会出现在 userViewModel 中。对不起,没有澄清!

4

3 回答 3

2

如果我理解正确,你需要类似的东西ExceptBy

childOrg.Users = users.ExceptBy(selectedUsers, u=>u.UserId)

public static class MyExtensions
{
    public static IEnumerable<T> ExceptBy<T, TKey>(
        this IEnumerable<T> list1,
        IEnumerable<T> list2,
        Func<T, TKey> keySelector)
    {
        HashSet<TKey> knownKeys = new HashSet<TKey>(list2.Select(x => keySelector(x)));
        return list1.Where(x => knownKeys.Add(keySelector(x)));
    }
}
于 2013-04-22T13:41:44.453 回答
1

我希望我已经理解了这个问题。
缓存您从方法中检索到的所有用户 ID Gateway.Instance.Map<User, OrganizationUserViewModel>,然后将缺少的用户 ID 添加到您的列表中:

foreach (var childOrg in viewModel.ChildOrganizations)
{
    var allUserId = new HashSet<int>();
    var users = this._organizationManager.GetOrganizationUsers(childOrg.OrganizationId);
    foreach (var user in users)
    {
        var userViewModel = Gateway.Instance.Map<User, OrganizationUserViewModel>(user.User);
        userViewModel.Organization_UserId = user.Organization_UserId;
        allUserId.Add(userViewModel.UserId);
    }

    var childOrgUsers = new List<OrganizationUserViewModel>();
    if (selectedUsers != null)
    {
        foreach (OrganizationUserViewModel selectedUser in selectedUsers)
        {
            if(allUserId.Contains(selectedUser.UserId) == false)
            {
                childOrgUsers.Add(userViewModel);
            }
        }
    }
    childOrg.Users = childOrgUsers;
}
于 2013-04-22T13:27:59.537 回答
1

好的。我想我明白你现在需要什么了。试试这个。

foreach (var childOrg in viewModel.ChildOrganizations)
{
    List<OrganizationUserViewModel> childOrgUsers = new List<OrganizationUserViewModel>();
    var users = this._organizationManager.GetOrganizationUsers(childOrg.OrganizationId);
    foreach (var user in users)
    {
        var userViewModel = Gateway.Instance.Map<User, OrganizationUserViewModel>(user.User);
        userViewModel.Organization_UserId = user.Organization_UserId;
        if (selectedUsers != null)
        {
            var foundUser = selectedUsers.FirstOrDefault(x => x.UserId == userViewModel.UserId);

            if(foundUser == null)
            {
                childOrgUsers.Add(userViewModel);
            }
        }
    }
    childOrg.Users = childOrgUsers.Distinct().ToList();
}
于 2013-04-22T13:41:02.177 回答