Sam Stephenson
Sam Stephenson

Reputation: 5190

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

Upvotes: 1

Views: 5075

Answers (3)

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

There is no value method. The statement buser.GetGroups() returns a PrincipalSearchResult collection.

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

You can then iterate over the collection,to fetch the names

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

Upvotes: 1

Habib
Habib

Reputation: 223277

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

Upvotes: 3

Neil Moss
Neil Moss

Reputation: 6838

Worth a shot:

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

Upvotes: 2

Related Questions