How to use LINQ to find out if any value in a ResultPropertyCollection contains a specific substring?

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.

+4
source share
4 answers

The type you are looking for is, I think, ResultPropertyValueCollection .

But you can not omit the type and use the output type?

 from myAddress in myResult.Properties["proxyAddresses"] 

Instead of using IndexOf(str) > 0 as your predicate, you can use Contains(str) .

+1
source

You will need to use something like this:

 var query = from myAddress in (myResult.Properties["proxyAddresses"] as IEnumerable).OfType<string>() where myAddress .ToLowerInvariant() .Contains("WeNeedThis.com".ToLowerInvariant()) select myAddress ; 

Does it help?

0
source

It worked for me

 DirectorySearcher mySearcher = new DirectorySearcher(mySearchRoot, myFilter, myPropertiesList); myResults = mySearcher.FindAll(); var query = myResults.Where(res =>res.GetDirectoryEntry().Properties["proxyAddresses"].Cast<string>().Any(addr => addr.Contains("WeNeedThis.com"))); 
0
source

System.DirectoryServices.ResultPropertyCollection property identifiers are of type Hashtable + Keycollection. I would drop it and check if it exists, and then extract the value from it:

 System.DirectoryServices.DirectorySearcher mySearcher = new DirectorySearcher(mySearchRoot, myFilter, myPropertiesList); System.DirectoryServices.SearchResultCollection myResults = mySearcher.FindAll(); foreach (System.DirectoryServices.SearchResult result in myResults) { var propertyNames = result.Properties.PropertyNames.Cast<String>().ToList<String>(); if (!propertyNames.Contains("proxyaddresses")) { // This property does not exist. Abort. continue; } List<string> proxyAddresses = result.Properties["proxyaddresses"].Cast<String>().ToList(); // Do something with proxyAddresses } 
0
source

Source: https://habr.com/ru/post/1380364/


All Articles