检查列表包含包含字符串元素,并得到该元素 [英] Check if list contains element that contains a string and get that element

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

问题描述

搜索在回答这个问题,我碰到类似的人使用LINQ,但我一直没能完全理解他们(因此,实现它们),因为我不熟悉它。我想,基本上是这样的:

  1. 检查列表中的任何元素都包含一个特定的字符串。
  2. 如果是这样,得到该元素。

我真的不知道我怎么会去这样做。我能想出的这个(不工作,当然):

 如果(myList.Contains(MyString的))
    字符串元= myList.ElementAt(myList.IndexOf(myString中));
 

我知道为什么它不工作:

  • myList.Contains()不返回,因为它会检查是否的整体元素我的列表中指定的字符串匹配。
  • myList.IndexOf()不会找到一个事件,因为,因为它是这样再次,它会检查匹配字符串的元素。

不过,我不知道如何解决这个问题,但我想我将不得不使用LINQ中类似的问题,以我的建议。话虽这么说,如果是这样的话在这里,我想对回答者向我解释他们的例子中,使用LINQ的(正如我所说,我没有打扰它在我的时间与C#)。谢谢你在前进的家伙(和女生?)。

编辑:我想出了一个解决方案;通过列表只是循环,检查当前元素包含字符串,然后设置一个字符串等于当前的元素。我不知道,不过,有没有比这更有效的方法?

 字符串MyString的=喇嘛;
字符串元素=;

的for(int i = 0; I< myList.Count;我++)
{
    如果(myList上[我]。载(MyString的))
        元= myList上[I]
}
 

解决方案

您应该可以在这里使用LINQ的:

  VAR matchingvalues​​ = myList中
    。凡(stringToCheck => stringToCheck.Contains(myString中));
 

如果您只是想返回第一个匹配项:

  VAR匹配= myList中
    .FirstOrDefault(stringToCheck => stringToCheck.Contains(myString中));

如果(匹配!= NULL)
    //做的东西
 

While searching for an answer to this question, I've run into similar ones utilizing LINQ but I haven't been able to fully understand them (and thus, implement them), as I'm not familiarized with it. What I would like to, basically, is this:

  1. Check if any element of a list contains a specific string.
  2. If it does, get that element.

I honestly don't know how I would go about doing that. What I can come up with is this (not working, of course):

if (myList.Contains(myString))
    string element = myList.ElementAt(myList.IndexOf(myString));

I know WHY it does not work:

  • myList.Contains() does not return true, since it will check for if a whole element of the list matches the string I specified.
  • myList.IndexOf() will not find an occurrence, since, as it is the case again, it will check for an element matching the string.

Still, I have no clue how to solve this problem, but I figure I'll have to use LINQ as suggested in similar questions to mine. That being said, if that's the case here, I'd like for the answerer to explain to me the use of LINQ in their example (as I said, I haven't bothered with it in my time with C#). Thank you in advance guys (and gals?).

EDIT: I have come up with a solution; just loop through the list, check if current element contains the string and then set a string equal to the current element. I'm wondering, though, is there a more efficient way than this?

string myString = "bla";
string element = "";

for (int i = 0; i < myList.Count; i++)
{
    if (myList[i].Contains(myString))
        element = myList[i];
}

解决方案

You should be able to use Linq here:

var matchingvalues = myList
    .Where(stringToCheck => stringToCheck.Contains(myString));

If you simply wish to return the first matching item:

var match = myList
    .FirstOrDefault(stringToCheck => stringToCheck.Contains(myString));

if(match != null)
    //Do stuff

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

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