Linq查询以检查列表和字符串之间的子字符串 [英] Linq query to check substring between a list and string

查看:93
本文介绍了Linq查询以检查列表和字符串之间的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表和一个字符串,我希望如果该字符串包含列表中存在的任何字符串,那么系统将返回false.我正在使用以下lambda表达式:

I have a list of strings and a string, and I want that if the string contains any of the string which is present in the list the system returns false. I am using following lambda expression:

 result = !(((string[])ro_operandVal).ToList<string>()).Any(x => x.ToString().ToUpper() == lo_operandVal.ToString().ToUpper());

以下是数据集:

lo_operandVal ="ABC DEF GHI邮政信箱" ro_operandVal是具有以下元素的字符串列表:

lo_operandVal = "ABC DEF GHI Post Office Box" ro_operandVal is the list of strings with following elements:

  1. 邮政信箱
  2. 邮政信箱BOX

当我评估该表达式时,它总是返回true.

when I evaluate this expression it always returns true.

推荐答案

您可以尝试以下linq- 如果在字符串中找到列表中的任何值,则它将返回false,如果找不到任何值,则它将返回true.

you can try the below linq - if any value in list found in string then it will return false, and if none of them found then it will return true.

var result = ro_operandVal.All(c=>lo_operandVal.IndexOf(c) < 0);

更新

都是对象类型,您知道它们的确切类型是List<string>string,可以键入强制转换它们.

both are object type and you know their exact type that they are List<string> and string you can type cast them.

var result = ((List<string>)ro_operandVal).All(c=>lo_operandVal.ToString().IndexOf(c) < 0);

这篇关于Linq查询以检查列表和字符串之间的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆