This project is read-only.

How to use PropertiesToLoad?

Jul 12, 2013 at 5:30 PM
In DirectoryServices we have the PropertiesToLoad collection on the DirectorySearcher object that helps us minimize unwanted LDAP properties in the result, thus improving performance.

What's the corresponding method to do that for a DirectoryContext Query in LINQ to LDAP?

/M
Jul 14, 2013 at 10:59 PM
Edited Jul 14, 2013 at 11:08 PM
Hi MagnusJ,

That is handled via the Select method. I've also included how it works using DirectoryServices.Protocols directly.
/* Static mapping */
//will retrieve all mapped properties.  The select is not needed when getting everything
List<User> results1 = context.Query<User>()
                            .Select(x => x)
                            .ToList();

//will retrieve only common name
List<string> results2 = context.Query<User>()
                            .Select(x => x.CommonName)
                            .ToList();

/* Dynamic mapping */
//will retrieve only the attributes requested
List<IDirectoryAttributes> results3 = context.Query("DC=server,DC=com")
                            .Select("cn", "objectGuid")
                            .ToList();

//will retrieve only the common name, but allows for using LINQ projections.
List<string> results4 = context.Query("DC=server,DC=com")
                            .Select(da => da.GetString("cn"))
                            .ToList();

/* Raw DirectoryServices.Protocols */
SearchRequest request = new SearchRequest();
request.DistinguishedName = "DC=server,DC=com";
request.Attributes.AddRange(new[]{"cn", "objectGuid"});
request.Scope = SearchScope.Subtree;
SearchResponse response = ldapConnection.SendRequest(request);