Methods

Where
You can chain any number of where clauses together

   var where = context.Query<User>()
        .Where(u => u.FirstName == "Sir" && 
                u.LastName == "Phobos" && 
                u.Title == "Knight of Mars");
 
   var where = context.Query<User>()
        .Where(u => u.FirstName == "Sir")
        .Where(u => u.LastName == "Phobos")
        .Where(u => u.Title == "Knight of Mars");
First / Single / Any / Count
These are immediate execution methods. I support all of these including the "OrDefault" variations. Single() and SingleOrDefault() will throw exceptions accordingly. First() will not throw an exception if there are no elements in the collection so it behaves the same as FirstOrDefault(). All of these methods make use of LDAP paging controls. If the server does not support paging, then it will fail. You can use any of these methods in combination with Where and Select. However, if you use Select then order does matter depending on the Select projection. For instance:

   //this works
   var where = context.Query<User>()
   	  .Where(u => u.FirstName == "Alan")
   	  .Select(u => new User{FirstName = u.FirstName, LastName = u.LastName})
   	  .FirstOrDefault(u => u.LastName == "Hatter");
    
   //this works because the property names on the anonymous projection match the mapped class.
   var where = context.Query<User>()
      .Where(u => u.FirstName == "Alan")
   	  .Select(u => new {FirstName = u.FirstName, LastName = u.LastName})
   	  .FirstOrDefault(u => u.LastName == "Hatter");
    
   //this will fail because the property name Last does not match LastName
   var where = context.Query<User>()
      .Where(u => u.FirstName == "Alan")
      .Select(u => new User{FirstName = u.FirstName, Last = u.LastName})
      .FirstOrDefault(u => u.Last == "Hatter");
ListAttributes and ListServerAttributes
ListAttributes allows you to list all attributes found and ListServerAttributes is a variation of ListAttributes that allows you to see information about the LDAP server.

   using (LdapConnection connection = new LdapConnection("localhost"))
   {
      var example = new { Cn = "" };
      using (var context = new DirectoryContext(connection))
      {
         IEnumerable<KeyValuePair<string, IEnumerable<KeyValuePair<string, object>>>> entries = 
            context.Query(example, "CN=Users,DC=Northwind,DC=local")
                 .Where(u => u.Cn == "John Doe")
                 .ListAttributes();
      }
   }

   using (LdapConnection connection = new LdapConnection("localhost"))
   {
      using (var context = new DirectoryContext(connection))
      {
         IDirectoryAttributes attributes = context.ListServerAttributes();
      }
   }
WithControls
LDAP controls can be used to modify the behavior of a search request. ScopedToAttribute and SearchWith are shortcuts that use WithControls to create a AsqRequestControl and SearchOptionsControl respectively.

using (var context = new DirectoryContext())
{
    var query = context.Query<Group>()
        .WithControls(new DirectoryControl[] { ... });
}
IgnoreOC / IncludeOC
These methods give you the ability to change if the mapped ObjectClasses and ObjectCategory will be included or ignored from a query. See Class Map, Attribute Class Map, or Query-By-Example for more information about mapping ObjectCategory and ObjectClasses.

using (var context = new DirectoryContext())
{
    var query = context.Query<User>()
        .IgnoreOC()
        .Where(u => Filter.Equal(u, "objectCategory", "computer"));

    var query2 = context.Query<User>()
         .IncludeOC(OC.ObjectClasses);
}
GetRequest
This will evaluate the query and return the raw SearchRequest object.

   SearchRequest request = context.Query<User>()
   	  .Where(u => u.FirstName == "Alan")
   	  .Select(u => new User{FirstName = u.FirstName, LastName = u.LastName})
   	  .GetRequest();

Last edited Oct 28, 2012 at 4:00 AM by MadHatter22, version 11