title: Complex Search Filtering with PrincipalSearcher
body: <p>I am searching in Active Directory for specific <code>SamAccountName</code> values using the <code>PrincipalSearcher</code> object because I want to return <code>UserPrincipal</code>'s. I was wondering how I can apply two filters to that search; one being the beginning of the account name starts with xx and the other being it does not end with _c.  </p>
  
  <p>Currently I can search for all results beginning with xx by using <code>xx*</code> but I cannot figure out how to add another search term or even apply a search term for does not equal.  This is what I'm currently working with.</p>
  
  <pre><code>protected override void RunTests()
  {
      using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "NAME", "OU=OUName",OU=name,DC=name,DC=net"))
      {
          UserPrincipal searchTemplate = new UserPrincipal(context);
          searchTemplate.Enabled = true;
          searchTemplate.SamAccountName = "xx*";
  
          PrincipalSearcher search = new PrincipalSearcher(searchTemplate);
  
          var principals = search.FindAll();
          int total = principals.Count();            
  
          int numInvalidUsers = RunChecks(principals, new Check[]{ 
              Check1    
              , Check2
              , Check3
          });
  
          Score = numInvalidUsers == 0 ? 1 : 0;
      }
  }
  </code></pre>
  
  <p>What I'm thinking is that I need to add another parameter to the <code>searchTemplate.SamAccountName</code>, I'm just not sure how.</p>
  
  <p><strong>Update:</strong>
  I was talking to someone on <a href="https://www.reddit.com/r/CodingHelp/comments/53zc4j/searching_active_directory_with_userprincipal/" rel="nofollow">Reddit</a> who was giving me some helpful suggestions but this user has gone dark.  It seems like the most common suggestion is to somehow implement LDAP filters.  So if anyone knows how to implement those while still returning principal objects that would be very helpful.</p>