【问题标题】:When doing foreach for Sharepoint 2010 UserProfiles, is there a way to sort them?为 Sharepoint 2010 UserProfiles 执行 foreach 时,有没有办法对它们进行排序?
【发布时间】:2012-09-06 13:42:05
【问题描述】:

我正在为 Sharepoing 2010 制作一个简单的生日日历 webpart。

我需要遍历网站的所有用户并显示在所述日期过生日的用户。我需要按用户姓名的字母顺序显示用户。

我可以通过这样做来遍历用户列表:

SPSite currentSite = ...
ServerContext ospServerContext = ServerContext.GetContext(currentSite);
UserProfileManager ospUserProfileManager = new UserProfileManager(ospServerContext);
foreach (UserProfile ospUserProfile in ospUserProfileManager) { ... }

但是,我无法控制配置文件的顺序。是否有任何内置方法可以通过一些简单的规则对配置文件进行排序,或者我是否必须制作一个 Dictionary(string,UserProfile),从 UserProfileManager 填充它,然后按其键对其进行排序,然后为其成员执行 foreach?

谢谢!

【问题讨论】:

    标签: c# visual-studio sharepoint sharepoint-2010 web-parts


    【解决方案1】:

    使用块添加:

    using System.Linq;
    

    创建用户配置文件集合:

    var collection = new List<UserProfile>();
    

    填充它:

    foreach (UserProfile ospUserProfile in ospUserProfileManager) 
    { 
       collection.Add(ospUserProfile);
    }
    

    然后sort it:

    var sorted = collection.OrderBy(p => p.PropertyToSort);
    

    【讨论】:

    • 嗯,很奇怪,但是 VS2010 抱怨 OrderBy 不是 List 的有效成员...我做错了什么?
    • 如果你打算走 LINQ 路线,你不妨全力以赴并使用 var collection = ospUserProfileManager.Cast&lt;UserProfile&gt;().ToList() 而不是 foreach 循环,或者更好的是直接从 @987654328 @ 进入OrderBy 而不创建List。并在OrderBy 之前进行生日过滤Where 以提高效率...
    • ToList() 等待 IEnumerable 源,但 ospUserProfileManager 不继承它。
    • 我的错误,我认为如果你可以在上面调用foreach,你可以调用Cast&lt;&gt; 来获得一个类型良好的枚举器。
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多