加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用LINQ查找ResultPropertyCollection中的任何值是否

发布时间:2020-12-15 21:25:37 所属栏目:百科 来源:网络整理
导读:如何使用LINQ查找ResultPropertyCollection中的任何值是否包含某个子字符串? 背景:在重新命名我的雇主公司后,我想检查所有用户是否都有一个正确的新电子邮件别名,这些别名在Active Directory中列为proxyAddresses. 直接访问ResultPropertyCollection中的单
如何使用LINQ查找ResultPropertyCollection中的任何值是否包含某个子字符串?

背景:在重新命名我的雇主公司后,我想检查所有用户是否都有一个正确的新电子邮件别名,这些别名在Active Directory中列为proxyAddresses.
直接访问ResultPropertyCollection中的单个值可以正常工作,如下所示:

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.
范围变量的类型确实是String = myResult.Properties [“proxyAddresses”] [0] .GetType(),尽管Collection没有直接的String成员.我误解了生成的编译器错误.

解决方法

你想要的类型我认为是 ResultPropertyValueCollection.

但是,你不能省略类型和使用类型推断吗?

from myAddress in myResult.Properties["proxyAddresses"]

也代替使用IndexOf(str)> 0作为谓词,你可以使用Contains(str).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读