加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何确保用户只被添加到列表一次

发布时间:2020-12-15 19:50:07 所属栏目:百科 来源:网络整理
导读:我必须通过自定义搜索列出用户列表,其中我将所有组中的所有用户添加到sharepoint web上的权限列表中.我的问题是用户可以在多个组中,因此他们会多次添加到返回的列表中.我如何确保它们只被添加一次? C# // keywords is the whatever value a user types into
我必须通过自定义搜索列出用户列表,其中我将所有组中的所有用户添加到sharepoint web上的权限列表中.我的问题是用户可以在多个组中,因此他们会多次添加到返回的列表中.我如何确保它们只被添加一次?

C#

// keywords is the whatever value a user types into the search textbox
private static IEnumerable<SPUser> GetUsers(SPWeb web,string keywords)
    {
        var oList = new List<SPUser>();
        var oListUsers = web.Groups.Cast<SPGroup>().SelectMany(grp => grp.Users.Cast<SPUser>().Where(user => user.Name.Contains(keywords))).ToList();

        foreach (SPUser user in oListUsers)
        {
            // My attempt here is to check if the list already contains the current item
            // but it seems to ignore it. I've tried counting too,but same outcome.
            if (!oList.Contains(user))
                oList.Add(user);
        }

        return oList;
    }

解决方法

试试这个(而不是包含)

if (! oList.Any(u => u.Name == user.Name ))
 {
      oList.Add(user);
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读