Tuesday, December 21, 2010

LINQ to LDAP: It's Alive!

Basic query by example using auto-mapping

using (LdapConnection connection = new LdapConnection("localhost"))
{
    var example = new {DistinguishedName = "", Cn = ""};
    using (var context = new DirectoryContext(connection))
    {
        context.Query(example, 
                      "CN=Users,CN=Employees,DC=Northwind,DC=local")
               .FirstOrDefault(u => u.Cn == "Alan Hatter");
    }
}

using (LdapConnection connection = new LdapConnection("localhost"))
{
    var example = new { DistinguishedName = "", Cn = "" };
    using (var context = new DirectoryContext(connection))
    {
        context.Query(example, 
                      "CN=Users,CN=Employees,DC=Northwind,DC=local")
               .ToList();
    }
}