How to use PropertiesToLoad?

Jul 12, 2013 at 4: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?

Jul 14, 2013 at 9:59 PM
Edited Jul 14, 2013 at 10: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)

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

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

//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"))

/* 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);