title: How to get distinct departments name from Active Directory in Asp .Net?
body: <p>I want to get unique or distinct departments name from AD so what is the correct query for that. Below is my code:</p>
  
  <pre><code>private static DomainController GetDomainController(string domainpath)
  {
      var domainContext = new DirectoryContext(DirectoryContextType.Domain, domainpath);
      var domain = Domain.GetDomain(domainContext);
      var controller = domain.FindDomainController();
      return controller;
  }
  
  private static MyMethod()
  {
      var domainController = GetDomainController(ActiveDirectorySettings.DomainPath);
  
      // Lookup the information in AD
      var ldapEntry = new DirectoryEntry(string.Format("LDAP://{0}", domainController)) { AuthenticationType = AuthenticationTypes.Secure | AuthenticationTypes.FastBind };
      DirectorySearcher ds;
  
      ds = new DirectorySearcher(ldapEntry)
      {
          SearchScope = SearchScope.Subtree,
          Filter = "(&amp;" + "(objectClass=user)" + "(department=" + departmentname + "*))"
      };
  
      ds.PropertiesToLoad.Add("department");
      if (ds.FindAll().Count &gt;= 1)
      {
          //do something....
      }
  }
  </code></pre>
  
  <p>Here I am getting all the departments name including duplicates. But what I want is -</p>
  
  <p>If 3 users belongs to the same department(X) then I want department(X) once not thrice.</p>