LINQ to AD – Grade B

Friday, August 15 2008 - , ,

There is a LINQ to AD project on CodePlex. Its a project that has some good merit to it, especially since AD is so horrible to query. LINQ to AD allows you to query a AD structure for groups and users using LDAP. So you can write a query like this to get all users whose first names’ start with the letter B:

var users = new DirectorySource<User>(ROOT, SearchScope.Subtree);
 
var res = from usr in users
          where usr.FirstName.StartsWith("B") 
          select new { Name = usr.FirstName + " " + usr.LastName, usr.LogonCount };
 
foreach (var u in res)
{
    Console.WriteLine(u.Name);
}

You can also query groups, too. The lacking part IMO is that it is not easy to query Users and Groups together. It would be great to be able to join the 2 in query since users can be in several groups and a group can have several users. There are obvious ways around this without using LINQ even. But here is to hoping the project contributors will add some support for joining the 2 structures. But I know how hard it can be to find some free time for projects like this :-)

Overall its a good tool and I tried using it recently for a project where I need to cycle through some queries for users and groups. Give it a try if you have not already.

DotNetKicks Image

4 comment(s)

Pingback from Dew Drop - August 16, 2008 | Alvin Ashcraft's Morning Dew

Thanks for checking out this project and for the suggestion. In fact, this is one of the things we have been considering but the underyling query capabilities of LDAP/AD are quite limited to express such queries for server-side execution. Therefore, lifting the join operator into the implementation would create a false sense of safety with regards to remote execution as we'd have to fall back to local execution. In such a case I personally prefer explicitness using AsEnumerable() to indicate the transition point from remote to local execution.

Hope this makes sense,

-Bart

Bart - Yep, makes sense. Thanks for responding. Isee why the difficulty exists with joining the sets. Actually I am just thrilled to have LINQ to AD to hide osme of the ugliness of AD querying. Thanks for your efforts!

Buck wrote on Tuesday, March 31 2009

I'm very interested in figuring out the whole LINQ to AD. Can you point me to any sources for someone just starting? e.g. Simple, hello world type example

Thanks.