I’ve written a query to read some stuff from Active Directory, but the result is some kind of "ResultPropertyCollection", which is something I’m not familiar with.
How can I convert this result to a list (like Generic List) that I’m more familiar with so I can do something with the results?
DirectoryEntry de = new DirectoryEntry("LDAP://" + this.rootLDAP);
DirectorySearcher ds = new DirectorySearcher(de, "(& (objectcategory=Group))");
ds.PropertiesToLoad.Add("samaccountname");
ds.PropertiesToLoad.Add("memberof");
ds.PropertiesToLoad.Add("samaccounttype");
ds.PropertiesToLoad.Add("grouptype");
ds.PropertiesToLoad.Add("member");
ds.PropertiesToLoad.Add("objectcategory");
var r = ( from SearchResult sr in ds.FindAll() select sr ) .ToArray();
If you only want a list of
SearchResulttypes, you could use:But since you want the actual values from the search results, you need to do more work.
Basically, that collection contains all the properties you’ve defined for the search – at least as long as they contain a value!
So really, what you need to do is create a class to hold those values. The two elements
memberOfandmembercan themselves contain multiple values (they’re "multi-valued" attributes in AD) – so you’ll need a list of strings for these:Then, once you have your search result, you need to iterate over the results and create new instances of
YourTypefor each search result, and stick those into aList<YourType>:and in that method, you need to inspect the
.Propertiescollection for each value and extract it: