繁体   English   中英

无法将类型System.DirectoryServices.AccountManagement.Principal隐式转换为字符串

[英]Cannot implicitly convert type System.DirectoryServices.AccountManagement.Principal to string

我有一个变量,它是一个字符串列表

var名称= new List();

我想将.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隐式转换为字符串

我想获取组的值并将其名称应用为字符串列表

值得一试:

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

没有像Value()方法/扩展方法那样的东西

如果要在字符串列表中获取用户名列表,可以执行以下操作:

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

没有价值的方法。 语句buser.GetGroups()返回PrincipalSearchResult集合。

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

然后,您可以遍历集合,以获取名称

  foreach(Principal pr in group)
     Console.WriteLine(pr.Name);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM