Filtering IQueryable<Users>

May 16, 2013 at 12:41 PM
First of all, great library.

I'm trying to filter with linq on the DistinguishedName of a User.

Situation:
 [DirectoryType("User")]
     public class User : UserEntryObject
    {
        [DirectoryProperty("samaccountname")]
        public string Name{ get; set; }
        [DirectoryProperty("distinguishedname")]
        public string DistinguishedName {get;set;}
    }

IQueryable<User> users; // containing a number of users. 
This fails to produce results.
users.Where(u => u.DistinguishedName.Contains("name") );
This works.
users.Where(u => u.Name.Contains("name") );
users.ToList().Where(u => u.DistinguishedName.Contains("name"));
Do you have any advice for me?
Coordinator
May 21, 2013 at 2:08 AM
Edited May 21, 2013 at 2:08 AM
Hello bmeer,

I too tried to query using a contains on distinguishedname and had similar results.
var test = all.Where(u => u.DistinguishedName.Contains("Stephen Baker")).ToArray();
I am assuming because the attribute in AD is a calculated value (when created or modified), readonly value, that behind the scenes, AD is handling the value in some other form besides a string.
"CN=Stephen Baker,OU=TestOU,DC=contoso,DC=com"
All though, querying by the full dn value works as expected.
var test = all.Where(u => u.DistinguishedName == "CN=Stephen Baker,OU=TestOU,DC=contoso,DC=com").ToArray();
http://technet.microsoft.com/en-us/library/cc977992.aspx

BTW, sorry for the late response, the email from codeplex went into my junk folder.

Sorry, I couldn't be much help with this one.

Thanks,
Stephen Baker
May 21, 2013 at 7:11 AM
Thanks for the reply.

For now I have a workaround but I will need to change it in the future.
Coordinator
May 21, 2013 at 7:22 AM
bmeer, do you mind sharing your work around? In case anyone else has the same problem with AD.

Thanks,
Stephen Baker

Coordinator
May 21, 2013 at 7:28 AM
Or, even better describe your problem in a little more detail so if there is a problem I / we can address it.

May 21, 2013 at 7:55 AM
The workaround is actually a little bit dirty and I know i can't keep it for a production application.

It's just converting the IQueryable to a List.

This will give me performance issues, so I know i will need to change it. But now I need to address some other, more urgent issues.

Thanks
Developer
Jul 16, 2013 at 8:59 AM
Unfortunately you cannot use wildcards in Distinguished Name searches. This is a feature of LDAP, not of this library. If you really need to search within the distinguished name for some reason, you will have to do as you are doing -- bring down the queryable as a list, and do the filtering on the client side.

Fortunately, it is rarely necessary to do searches on Distinguished Names. Without knowing anything about your AD setup (or your personal level of knowledge -- apologies if you know this already) it seems like you are trying search for particular users by name. You will see that the distinguished names have a particular format; for example, something like
distinguishedName=CN=Potts\, T.,OU=Users,DC=xxx,DC=yyy,DC=uk
Each section of the distinguished name is made from another attribute; in this case, the first part is the CN (common name) of the User object. If you are searching for someone then you would want to do a query like CN=*name*, which translates, in this library, to something like
Users.Where(u=>u.CommonName.Contains("name"))
If you want to be searching on anything other than just the relative distinguished name (i.e. the leftmost part of the DN) then you'll need to alter your query in some other way; for example, if you just want users from a particular organisational unit, then do a scoped search to just restrict your results to that OU.

I hope some of that was helpful!

Best wishes,
Tom
Marked as answer by sbaker on 10/7/2013 at 9:41 AM