2012-10-04 92 views
0

我有一個變量,它是一個字符串列表無法隱式轉換類型System.DirectoryServices.AccountManagement.Principal串

變種名稱=新名單();

我想一個.AccountManagement.Principal查詢結果的指定名稱名稱

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
UserPrincipal buser = UserPrincipal.FindByIdentity(ctx, user); 

if (buser != null) 
{ 
    var group = buser.GetGroups().Value().ToList(); 
    names = group; 
} 

顯然,這並不編譯,因爲.Value不是GetGroups()的屬性。

如果我嘗試

var group = buser.GetGroups().Value().ToList(); 
names = group; 

我得到

不能鍵入System.DirectoryServices.AccountManagement.Principal隱式轉換爲字符串

我想組的值和將其應用於名稱字符串列表

回答

2

有沒有像Value()方法/擴展方法

如果你想在字符串列表用戶名列表,你可以這樣做:

List<string> userList = buser.GetGroups().Select(r=>r.Name).ToList(); 
2

Worth一個鏡頭:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
UserPrincipal buser = UserPrincipal.FindByIdentity(ctx, user); 
if (buser != null) 
{ 
    names = (
     from 
      groupPrincipal in buser.GetGroups() 
     select 
      groupPrincipal.Name 
     ).ToList(); 
} 
1

有沒有價值的方法。 陳述buser.GetGroups()返回一個PrincipalSearchResult集合。

PrincipalSearchResult<Principal> group = buser.GetGroups(); 

然後,您可以遍歷集合,以獲取名稱

foreach(Principal pr in group) 
    Console.WriteLine(pr.Name); 
相關問題