检查列表中的字符串包含LINQ的一个特定的字符串 [英] Check if a string within a list contains a specific string with Linq

查看:524
本文介绍了检查列表中的字符串包含LINQ的一个特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表<串> 有一些项目是这样的:

I have a List<string> that has some items like this:

{"Pre Mdd LH", "Post Mdd LH", "Pre Mdd LL", "Post Mdd LL"}

现在我想执行的检查,如果在列表中的项目包含特定字符串的条件。是这样的:

Now I want to perform a condition that checks if an item in the list contains a specific string. something like:

如果列表包含包含this_string

要使它简单我想如果该列表中一气呵成至少检查!例如包含具有 MDD LH 在这一个项目

To make it simple I want to check in one go if the list at least! contains for example an item that has Mdd LH in it.

我的意思是这样的:

if(myList.Contains(str => str.Contains("Mdd LH))
{
    //Do stuff
}

感谢。

推荐答案

我想你想 任何

if (myList.Any(str => str.Contains("Mdd LH")))

这是很值得成为熟悉的LINQ标准查询运算符,我通常会使用这些,而不是具体的实现方法(如列表< T> ; .ConvertAll ),除非我确实是由特定的运算符的性能困扰(具体实现的方法有时可以通过知道结果等的尺寸是更有效的)

It's well worth becoming familiar with the LINQ standard query operators; I would usually use those rather than implementation-specific methods (such as List<T>.ConvertAll) unless I was really bothered by the performance of a specific operator. (The implementation-specific methods can sometimes be more efficient by knowing the size of the result etc.)

这篇关于检查列表中的字符串包含LINQ的一个特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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