2012-09-13 33 views
2

我正在爲Sharepoing 2010製作一個簡單的生日日曆webpart。在爲Sharepoint 2010 UserProfiles做foreach時,有沒有辦法對它們進行排序?

我需要遍歷網站的所有用戶並顯示那些在該日期有生日的人。我需要按用戶姓名的字母順序呈現用戶。

我可以做這個ILKE通過用戶列表進行迭代:

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

但是,後來我就在配置文件的順序無法控制。是否有任何內置的方式來通過一些簡單的規則排序配置文件,或者我必須做一個Dictionary(字符串,UserProfile),從UserProfileManager填充它,然後按鍵排序,然後對其成員進行foreach?

謝謝!

回答

3

添加使用塊:

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); 
+0

嗯它的奇怪,但VS2010抱怨OrderBy不是列表的有效成員...我做錯了什麼? – Istrebitel

+0

在答案更新中添加*使用*塊。 –

+0

如果你打算去LINQ路線,那麼你可以去整個豬羣,並使用'var collection = ospUserProfileManager.Cast ().ToList()'而不是'foreach'循環,或者更好的辦法是去直接從'Cast'轉換爲'OrderBy'而不用創建List。並在'OrderBy'之前進行生日過濾'Where'以提高效率... – Rawling

相關問題