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

前端 未结 12 2491
抹茶落季
抹茶落季 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:20
    for (int i = 0; i < myList.Length; i++)
    {
        if (myList[i].Contains(myString)) // (you use the word "contains". either equals or indexof might be appropriate)
        {
            return i;
        }
    }
    

    Old fashion loops are almost always the fastest.

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

    If you want a list of strings containing your string:

    var newList = myList.Where(x => x.Contains(myString)).ToList();
    

    Another option is to use Linq FirstOrDefault

    var element = myList.Where(x => x.Contains(myString)).FirstOrDefault();
    

    Keep in mind that Contains method is case sensitive.

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

    You should be able to use something like this, it has worked okay for me:

    var valuesToMatch = yourList.Where(stringCheck => stringCheck.Contains(myString));
    

    or something like this, if you need to look where it doesn't match.

     var valuesToMatch = yourList.Where(stringCheck => !stringCheck.Contains(myString));
    
    0 讨论(0)
  • 2020-12-07 11:29

    It is possible to combine Any, Where, First and FirstOrDefault; or just place the predicate in any of those methods depending on what is needed.

    You should probably avoid using First unless you want to have an exception thrown when no match is found. FirstOrDefault is usually the better option as long as you know it will return the type's default if no match is found (string's default is null, int is 0, bool is false, etc).

    using System.Collections.Generic;
    using System.Linq;
    
    
    bool exists;
    string firstMatch;
    IEnumerable<string> matchingList;
    
    var myList = new List<string>() { "foo", "bar", "foobar" };
    
    exists = myList.Any(x => x.Contains("o"));
    // exists => true
    
    firstMatch = myList.FirstOrDefault(x => x.Contains("o"));
    firstMatch = myList.First(x => x.Contains("o"));
    // firstMatch => "foo"
    
    firstMatch = myList.First(x => x.Contains("dark side"));
    // throws exception because no element contains "dark side"
    
    firstMatch = myList.FirstOrDefault(x => x.Contains("dark side"));
    // firstMatch => null
    
    matchingList = myList.Where(x => x.Contains("o")); 
    // matchingList => { "foo", "foobar" }
    

    Test this code @ https://rextester.com/TXDL57489

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

    Many good answers here, but I use a simple one using Exists, as below:

    foreach (var setting in FullList)
    {
        if(cleanList.Exists(x => x.ProcedureName == setting.ProcedureName)) 
           setting.IsActive = true; // do you business logic here 
        else
           setting.IsActive = false;
        updateList.Add(setting);
    }
    
    0 讨论(0)
  • 2020-12-07 11:31

    To keep it simple use this;

    foreach(string item in myList)//Iterate through each item.
    {
     if(item.Contains("Search Term")//True if the item contains search pattern.
     {
       return item;//Return the matched item.
     }
    }
    

    Alternatively,to do this with for loop,use this;

        for (int iterator = 0; iterator < myList.Count; iterator++)
        {
            if (myList[iterator].Contains("String Pattern"))
            {
                return myList[iterator];
            }
        }
    
    0 讨论(0)
提交回复
热议问题