Question

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

Was it helpful?

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

OTHER TIPS

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

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top