I had to search for displayName's which started with a "(". LDAP query syntax uses parenthesis and therefore can't handle this without replacing that and other special characters with the escape sequence substitutes (
http://msdn.microsoft.com/en-us/library/aa746475%28VS.85%29.aspx)
I added the following function to my copy of DirectorySource.cs
/// <summary>
/// Replaces the special characters.
/// </summary>
/// <param name="constantValue">The constant value.</param>
/// <returns></returns>
private object ReplaceSpecialCharacters(object constantValue)
{
if (constantValue is string)
{
StringBuilder returnValue = new StringBuilder((string)constantValue);
returnValue = returnValue.Replace(@"\", @"\5c");
returnValue = returnValue.Replace(@"*", @"\2a");
returnValue = returnValue.Replace(@"(", @"\28");
returnValue = returnValue.Replace(@")", @"\29");
returnValue = returnValue.Replace(@"/", @"\00");
return returnValue.ToString();
}
return constantValue;
}
And then modified ParsePredicate to contain the following:
switch (m.Method.Name)
{
case "Contains":
{
ConstantExpression c = m.Arguments[0] as ConstantExpression;
sb.AppendFormat("{0}=*{1}*", GetFieldName(o.Member), ReplaceSpecialCharacters(c.Value));
break;
}
case "StartsWith":
{
ConstantExpression c = m.Arguments[0] as ConstantExpression;
sb.AppendFormat("{0}={1}*", GetFieldName(o.Member), ReplaceSpecialCharacters(c.Value));
break;
}
case "EndsWith":
{
ConstantExpression c = m.Arguments[0] as ConstantExpression;
sb.AppendFormat("{0}=*{1}", GetFieldName(o.Member), ReplaceSpecialCharacters(c.Value));
break;
}
default:
throw new NotSupportedException("Unsupported string filtering query expression detected. Cannot translate to LDAP equivalent.");
}