LINQ to AD – Grade B

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.