Check if list contains element that contains a string and get that element

前端 未结 12 2541
抹茶落季
抹茶落季 2020-12-07 10:39

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

12条回答
  •  旧时难觅i
    2020-12-07 11:15

    The basic answer is: you need to iterate through loop and check any element contains the specified string. So, let's say the code is:

    foreach(string item in myList)
    {
        if(item.Contains(myString))
           return item;
    }
    

    The equivalent, but terse, code is:

    mylist.Where(x => x.Contains(myString)).FirstOrDefault();
    

    Here, x is a parameter that acts like "item" in the above code.

提交回复
热议问题