c# – 如何使用LINQ查找ResultPropertyCollection中的任何值是否
如何使用LINQ查找ResultPropertyCollection中的任何值是否包含某个子字符串?
背景:在重新命名我的雇主公司后,我想检查所有用户是否都有一个正确的新电子邮件别名,这些别名在Active Directory中列为proxyAddresses. 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; 但是我在搜索集合中的所有值时失败了.我似乎无法找出范围变量的正确类型是什么: where (from WhatType? myAddress in myResult.Properties["proxyAddresses"] where (myAddress.Contains("WeNeedThis.com")) select myAddress) 如何设置where子句,以便在任何valueAddresses值中找到搜索字符串的任何出现? 答案:原来这是正确的where子句: where ( ( from String myAddress in myResult.Properties["proxyAddresses"] where myAddress.Contains("WeNeedThis.com") select myAddress).ToList().Count == 0) 交织在一起有两个错误:外部where子句需要内部选择结果的布尔结果,这是通过.ToList()实现的.Count == 0. 解决方法
你想要的类型我认为是
ResultPropertyValueCollection.
但是,你不能省略类型和使用类型推断吗? from myAddress in myResult.Properties["proxyAddresses"] 也代替使用IndexOf(str)> 0作为谓词,你可以使用Contains(str). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |