Suggestion: DirectoryServicesContext


Looking at the sample, you're manually creating DirectorySources, passing in a common root. I suggest that you follow the lead of LINQ to SQL and create a DirectoryContext type which accepts the root URI, and which will auto-matically instantiate DirectorySource fields. You could also forward the setting of the Log property if you wanted.
Then you could have:
class LocalContext: DirectoryServicesContext
public DirectorySource<User> Users;
public LocalContext(string uri)
: base(uri) // instantiates and stores root, instantiates Users
static void Main(string[] args)
var ldap = new LocalContext("LDAP://localhost");
var res1 = from usr in ldap.Users select usr;
// and nothing to prevent you from creating a new DirectorySource within that context:
var myUsers = ldap.GetDirectorySource<MyUser>(SearchScope.SubTree);
var res2 = from myUser in myUsers select myUser;
Closed Jul 16, 2010 at 6:58 AM by bdesmet


bdesmet wrote Dec 3, 2007 at 4:42 AM

Hi Keith,

Thanks for the feedback - I'm aware of this issue but didn't consider it for implementation in the initial LINQ to LDAP sample series on my blog to reduce overhead. This RTM release is a straightforward adaption of the article series and hence the principle of having a data context hasn't made it to implementation. However, this principle has been applied in the LINQ to SharePoint project.

Over time I plan to update this project to follow the same design, maybe in a 1.1 release.


wrote Dec 17, 2007 at 12:42 PM

wrote Jul 16, 2010 at 6:58 AM

wrote Jul 16, 2010 at 6:58 AM

wrote Feb 14, 2013 at 6:57 PM

wrote May 16, 2013 at 9:18 AM