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

标签 c# linq sharepoint-2007

我必须通过自定义搜索列出用户列表,我将所有组中的所有用户添加到 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);
 }

关于c# - 如何确保用户只被添加到列表一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5792366/

相关文章:

html - 添加搜索核心结果 Web 部件后,IE 中的 mailto 中断

Sharepoint Wiki 链接名称

c# - Awesomium Winforms 在 VMWare 中不工作

javascript - 在 asp.net MVC 中更改文本框日期时的消防 Controller 代码

c# - 对于每次迭代,在一个类中获得相同的值

c# - linq 按日期时间列上的开始时间到结束时间分组

web-services - 共享点 2007 : How to change a custom webpart webservice URL?

c# - 模棱两可的方法调用c#

c# - 在 Lucene 中索引多个表

C# 将两个集合连接在一起