Formatting

Some directory attributes require formatting in order to correctly search against them. There are also certain characters that must be "cleaned".
DateTime
DateTimes in the directory can have two formats. LDAP time format and file time. You can map a DateTime by following the Data Types mapping and it will automatically get converted to the correct format. Alternatively you can format the date time using the Filter class or with a custom filter.

IQueryable<User> query = context.Query<User>()
     .Where(u => u.WhenCreated == DateTime.Now);
//filter: (WhenCreated=20101124000000.0Z)

IQueryable<User> query = context.Query<User>()
     .Where(u => Filter.Equal(u, u.LastChanged, DateTime.Now.ToFileTime().ToString()));
//filter: (LastChanged=129457587879957082)
Byte Array / Guid / Security Identifier
Byte Array based properties have to be converted to a string octect for querying. Mapped properties will automatically be converted for you, but if you are using custom filters then you'll have to use the extension method ToStringOctet.

IQueryable<User> query = context.Query<User>()
     .Where(u => u.Guid == Guid.NewGuid());
//filter: (objectguid=\03\dd\78\f2\d4\e5\2a\4d\8d\a8\9b\43\64\55\85\a3)
Special Characters and Null
There are certain reserved characters that are not valid for a filter. I clean '\', '*', '(', ')', and '\u0000' for all filter values except when using custom filters. Null is also a special value. Null in .Net doesn't have the same meaning as no value in LDAP. Whenever a null is encountered in a filter, I convert it to an "equal to anything" or not "equal to anything".

var complex = context.Query<User>()
    .Where(u => u.Title == null || u.PhoneNumber == "(206) 555-3412");
 
//filter: (|(!(Title=*))(telephonenumber=\\28206\\29 555-3412))

Last edited Nov 18, 2013 at 3:00 AM by MadHatter22, version 6