简体   繁体   English

检查字符串列表是否包含项目

[英]Check if a string list contains an item

I have a string list like; 我有一个类似的字符串列表;

public static readonly List<string> myList = new List<string>
{
    "123",
    "456",
    "789"
};

And I have a method, let's say MethodX(string). 我有一个方法,比方说MethodX(string)。 MethodX(string) is looking for and compares entered string in another string list that comes from API. MethodX(string)正在寻找并比较来自API的另一个字符串列表中输入的字符串。 It returns true if an entered string matches any string from the list. 如果输入的字符串与列表中的任何字符串匹配,则返回true。

Now what I'm doing is; 现在我正在做的是

var y = myList;
foreach (var x in y)
{
    var asd = me.MethodX(x);
    if (asd == true)
    {
        // Do stuff
    }
}

I want every member of the string list to be entered MethodX() and check if it's true. 我希望将字符串列表的每个成员输入MethodX()并检查其是否正确。 But currently it only tries "123" as the first member of myList. 但是目前,它仅尝试将“ 123”作为myList的第一个成员。

How can I make it happen? 我该如何实现?

bool MethodX(this List<string>,string x)
{
   return this.Any(s=>s==x);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM