How to use LINQ to determine if a value in a ResultPropertyCollection contains a specific substring?
Reference Information. After renaming my employer, I want to check if all users have the correct new email alias, which is listed as proxyAddresses in Active Directory. Direct access to individual values inside ResultPropertyCollection works just fine, for example:
DirectorySearcher mySearcher = new DirectorySearcher(mySearchRoot, myFilter, myPropertiesList); myResults = mySearcher.FindAll(); var query = from SearchResult myResult in myResults where (myResult.Properties["proxyAddresses"][0].ToString().Contains ("WeNeedThis.com")) select myResult;
But I can’t search all the values inside the collection. I don't seem to know what the correct type is for a range variable:
where (from WhatType? myAddress in myResult.Properties["proxyAddresses"] where (myAddress.Contains("WeNeedThis.com")) select myAddress)
How can I configure the where clause to find any appearance of the search string in any proxy value?
Answer: It turns out this is a where clause:
where ( ( from String myAddress in myResult.Properties["proxyAddresses"] where myAddress.Contains("WeNeedThis.com") select myAddress).ToList().Count == 0)
Two errors were intertwined: the outer phrase where requires a logical result from the result of the internal selection, which is achieved using .ToList (). Count == 0. The type of the range variable is really String = myResult.Properties ["proxyAddresses"] [0] .GetType (), although there are no direct String members in the collection. I misinterpreted the compiler error that occurred.
source share