How would i assign only DisplayName and Name from PrincipalSearcher to List?

413 Views Asked by At

I have the following sample code:

namespace DirectorySearcher
{
  class Program
  {
    static void Main(string[] args)
    {
        using (var context = new PrincipalContext(ContextType.Domain, "bobo.net"))
        {
            using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { Enabled = true }))
            {
                List<Principal> results = new List<Principal>();
                results.AddRange(searcher.FindAll());
                //foreach (var result in searcher.FindAll())
                //{                        
                    //Console.WriteLine("displayName : " + result.DisplayName);
                    //Console.WriteLine("name : " + result.Name);
                    //Console.WriteLine();
                //}
            }
        }
        Console.WriteLine("Done");
        Console.ReadLine();
    }
  }
}

how would I assign only the result.DisaplayName and result.Name to the results list?

can you configure the PrincipalSearcher to only pull back those two values to start with

like you can do with PowerShell ie:

 Get-ADUser -Properties DisplayName,Name

Thanks

1

There are 1 best solutions below

1
On BEST ANSWER
var list = searcher.FindAll().Select(s => new {name = s.Name, displayName = s.DisplayName});

The above creates a List of anonymous objects, that contains name and displayname. You could create a poco class to hold the name and display name instead

For limiting the properties of the response look at this question