如何确保用户只添加到列表一次

发布于 2024-11-03 04:24:11 字数 869 浏览 2 评论 0原文

我必须通过自定义搜索列出用户列表,其中我从添加到共享点 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;
    }

I have to list a list of users through a custom search, where I take all the users from all the groups added to the permissions list on the sharepoint web. My issue is that users can be in several groups, thus they get added multiple times to the list that gets returned. How do I make sure they only gets added once?

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

再浓的妆也掩不了殇 2024-11-10 04:24:11

试试这个(而不是包含)

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

Try this (instead of contains)

 if (! oList.Any(u => u.Name == user.Name ))
 {
      oList.Add(user);
 }
七七 2024-11-10 04:24:11

看起来您的 SPUser 类需要实现 IEquatable 才能包含按您想要的方式工作。

Looks like your SPUser class needs to implement IEquatable<SPUser> for contains to work as you want it.

離人涙 2024-11-10 04:24:11

您可以使用 Linq 获取唯一记录

var uniqueValues = oList.Distinct();

这将删除具有相同引用的 SPUser 对象。您还可以为自己的平等逻辑实现 IEqualityCompaprer

You can use Linq to get unique records

var uniqueValues = oList.Distinct();

This will remove SPUser object with same reference. Also you can implement IEqualityCompaprer<SPUser> for your own equlity logic

烂柯人 2024-11-10 04:24:11

使用哈希集而不是列表。这样,您就不必检查是否包含,并且重复的项目将被忽略。

这也会更快,因为 HashSet 几乎可以轻松拒绝重复项,而 List<>.Contains() 的复杂度为 O(n)

 var oList = new HashSet<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)
    {
      oList.Add(user);
    }

Use a Hashset instead of a List. That way, you don't have to check for containment, and duplicate items will just be ignored.

This will be faster too, as the HashSet is able to reject duplicates almost trivially, while the List<>.Contains() is O(n)

 var oList = new HashSet<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)
    {
      oList.Add(user);
    }
王权女流氓 2024-11-10 04:24:11

通过暴力,你可以使用 Distinct:

var oListUsers = web.Groups.Cast<SPGroup>().SelectMany(grp => grp.Users.Cast<SPUser>().Where(user => user.Name.Contains(keywords))).Distinct().ToList();

By brute force, you could just use Distinct:

var oListUsers = web.Groups.Cast<SPGroup>().SelectMany(grp => grp.Users.Cast<SPUser>().Where(user => user.Name.Contains(keywords))).Distinct().ToList();
心在旅行 2024-11-10 04:24:11

问题是您的 oListUsers 列表中有不同的对象代表同一用户,但具有不同的对象引用 - 由于 Contains() 使用对象引用进行检查,因此您赢了除非您在 SPUser 类上定义自定义比较器/平等,否则无法捕获这种情况。

或者,如果用户名是唯一的,您可以通过这种方式过滤掉重复项。

The problem is that you have different objects in your oListUsers list that represent the same user, but have different object references - since Contains() uses object references to check, you won't be able to catch this case unless you define a custom comparer / Equality on your SPUser class.

Alternatively if i.e. the user name is unique you could filter out duplicates that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文