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

前端 未结 12 2534
抹茶落季
抹茶落季 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: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.

提交回复
热议问题