Project Description
LINQ to AD (Active Directory) provides ways of querying Active Directory using LINQ syntax and Lambda expressions.

Nuget Package
http://nuget.org/packages/System.DirectoryServices.Linq
PM> Install-Package System.DirectoryServices.Linq

The following IQueryable<T> methods are supported:

NEW
  • Count
    • users.Count(u => u.FirstName == "Stephen");
    • users.Where(u => u.FirstName == "Stephen").Count();
Previously Released
  • Where
    • users.Where(u => u.UserName == "MyUserName");
    • users.Where(u => u.UserName == "MyUserName" || u.FirstName == "Stephen");
    • users.Where(u => u.UserName == "MyUserName").Where(u => u.FirstName == "Stephen");
    • users.Where(u => u.UserName.StartsWith("MyUser"));
    • users.Where(u => u.UserName.EndsWith("serName"));
    • users.Where(u => u.UserName.Contains("UserNa"));
  • Select
    • users.Where(u => u.LastName == "Baker").Select(u => new{Name = string.Concat(u.FirstName, " ", u.LastName)});
  • First / FirstOrDefault
    • users.First(u => u.LastName == "Baker");
    • users.Where(u => u.UserName == "MyUserName").First();
    • users.FirstOrDefault(u => u.LastName == "Baker");
    • users.Where(u => u.UserName == "MyUserName").FirstOrDefault();
  • Single / SingleOrDefault
    • users.Single(u => u.LastName == "Baker");
    • users.Where(u => u.UserName == "MyUserName").Single();
    • users.SingleOrDefault(u => u.LastName == "Baker");
    • users.Where(u => u.UserName == "MyUserName").SingleOrDefault();
  • Last / LastOrDefault
    • users.Last(u => u.LastName == "Baker");
    • users.Where(u => u.UserName == "MyUserName").Last();
    • users.LastOrDefault(u => u.LastName == "Baker");
    • users.Where(u => u.UserName == "MyUserName").LastOrDefault();
  • OrderBy / OrderByDecending
    • users.Where(u => u.FirstName == "Stephen").OrderBy(u => u.LastName);
    • users.Where(u => u.FirstName == "Stephen").OrderByDecending(u => u.LastName);
  • Skip / Take
    • users.Where(u => u.FirstName == "Stephen").Skip(10);
    • users.Where(u => u.FirstName == "Stephen").Take(25);
    • users.Where(u => u.FirstName == "Stephen").Skip(10).Take(25);
  • Querys
    • var users = (from u in context.Users where u.UserName == "MyUserName" select u);

For more information, see the documentation tab.

Please, let me know what you think and if you have any question or comments in the discussions tab.

Thanks for trying LINQ to AD,

Last edited Dec 18, 2012 at 3:28 AM by sbaker, version 19