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

前端 未结 12 2490
抹茶落季
抹茶落季 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条回答
  • 2020-12-07 11:06
    string result = myList.FirstOrDefault(x => x == myString)
    if(result != null)
    {
      //found
    }
    
    0 讨论(0)
  • 2020-12-07 11:10

    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
    
    0 讨论(0)
  • 2020-12-07 11:11

    You could use Linq's FirstOrDefault extension method:

    string element = myList.FirstOrDefault(s => s.Contains(myString));
    

    This will return the fist element that contains the substring myString, or null if no such element is found.

    If all you need is the index, use the List<T> class's FindIndex method:

    int index = myList.FindIndex(s => s.Contains(myString));
    

    This will return the the index of fist element that contains the substring myString, or -1 if no such element is found.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 11:15

    you can use

    var match=myList.Where(item=>item.Contains("Required String"));
    foreach(var i in match)
    {
    //do something with the matched items
    }
    

    LINQ provides you with capabilities to "query" any collection of data. You can use syntax like a database query (select, where, etc) on a collection (here the collection (list) of strings).

    so you are doing like "get me items from the list Where it satisfies a given condition"

    inside the Where you are using a "lambda expression"

    to tell briefly lambda expression is something like (input parameter => return value)

    so for a parameter "item", it returns "item.Contains("required string")" . So it returns true if the item contains the string and thereby it gets selected from the list since it satisfied the condition.

    0 讨论(0)
  • 2020-12-07 11:17

    I have not seen bool option in other answers so I hope below code will help someone.

    Just use Any()

    string myString = "test";
    bool exists = myList
                 .Where(w => w.COLUMN_TO_CHECK.Contains(myString)).Any();
    
    0 讨论(0)
提交回复
热议问题