Search code examples
c#account-management

Cannot implicitly convert type System.DirectoryServices.AccountManagement.Principal to string


I've got a variable which is a list of strings

var names = new List();

I want to assign the names of the results of a .AccountManagement.Principal query to names

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

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

Obviously this does not compile, since .Value is not a property of GetGroups().

If I try

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

I get

Cannot implicitly convert type System.DirectoryServices.AccountManagement.Principal to string

I want to get the value of group and apply it the name a list of strings


Solution

  • There is nothing like Value() method/extension method

    If you want to get a list of UserNames in a list of string you can do:

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