防止foreach语句中的多个条目

时间:2013-04-22 13:18:37

标签: c# asp.net asp.net-mvc-3

以下代码填充了不属于现有列表“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中的当前条目不匹配时为所选用户添加条目。这些是我真正需要专注于删除的内容,因为我希望userUMadl中不存在存储在selectedUsers中的任何内容。很抱歉没有澄清!

3 个答案:

答案 0 :(得分: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)));
    }
}

答案 1 :(得分:1)

我希望我理解这个问题 从方法Gateway.Instance.Map<User, OrganizationUserViewModel>缓存您检索的所有用户ID,然后将缺少的用户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;
}

答案 2 :(得分: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();
}