如何确保用户只添加到列表一次
我必须通过自定义搜索列出用户列表,其中我从添加到共享点 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个(而不是包含)
Try this (instead of contains)
看起来您的 SPUser 类需要实现
IEquatable
才能包含按您想要的方式工作。Looks like your SPUser class needs to implement
IEquatable<SPUser>
for contains to work as you want it.您可以使用 Linq 获取唯一记录
这将删除具有相同引用的
SPUser
对象。您还可以为自己的平等逻辑实现IEqualityCompaprer
You can use Linq to get unique records
This will remove
SPUser
object with same reference. Also you can implementIEqualityCompaprer<SPUser>
for your own equlity logic使用哈希集而不是列表。这样,您就不必检查是否包含,并且重复的项目将被忽略。
这也会更快,因为 HashSet 几乎可以轻松拒绝重复项,而 List<>.Contains() 的复杂度为 O(n)
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)
通过暴力,你可以使用 Distinct:
By brute force, you could just use Distinct:
问题是您的 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 - sinceContains()
uses object references to check, you won't be able to catch this case unless you define a custom comparer / Equality on yourSPUser
class.Alternatively if i.e. the user name is unique you could filter out duplicates that way.