Check if the list contains an element containing a string and get this element

When searching for an answer to this question, I came across similar ones using LINQ, but I could not fully understand them (and therefore implement them), since I am not familiar with it. Basically I would like to:

  • Check if any list item contains a specific string.
  • If so, get this item.

Honestly, I do not know how to do this. I can come up with this (doesn't work, of course):

if (myList.Contains(myString)) string element = myList.ElementAt(myList.IndexOf(myString)); 

I know WHY this does not work:

  • myList.Contains() does not return true , as it will check whether the entire list item matches the specified string.
  • myList.IndexOf() will not find an entry, as, as in the case with this, it will check the element corresponding to the string.

However, I do not know how to solve this problem, but I believe that I will have to use LINQ, as suggested in similar questions, for mine. Moreover, if this is the case, I would like the respondent to explain to me the use of LINQ in their example (as I said, I did not worry about this in my time with C #). Thanks in advance, guys (and girls?).

EDIT: I came up with a solution; just go through the list, check if the current item contains a string, and then set the string equal to the current item. I am interested, however, is there a more efficient way than this?

 string myString = "bla"; string element = ""; for (int i = 0; i < myList.Count; i++) { if (myList[i].Contains(myString)) element = myList[i]; } 
+58
contains list c # linq
Sep 12 '13 at 2:39 on
source share
8 answers

You can use Linq here:

 var matchingvalues = myList .Where(stringToCheck => stringToCheck.Contains(myString)); 

If you just want to return the first matching element:

 var match = myList .FirstOrDefault(stringToCheck => stringToCheck.Contains(myString)); if(match != null) //Do stuff 
+94
Sep 12 '13 at 14:44
source share

The main answer: you need to go through the loop and check if any element contains the specified string. So, let's say that the code

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

Equivalent But Short Code

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

Here x is a parameter that acts as an element in the code above.

+15
Sep 12 '13 at 14:46
source share
 string result = myList.FirstOrDefault(x => x == myString) if(result != null) { //found } 
+5
Sep 12 '13 at 14:43
source share
 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 mods are almost always the fastest.

+5
Sep 12 '13 at 14:44
source share

If you need 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 the Contains method is case sensitive.

+3
Sep 12 '13 at 14:44
source share

You can use the Linq FirstOrDefault extension method:

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

This will return a fist element containing the substring myString or null if no such element is found.

If you only need an index, use the List<T> class FindIndex :

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

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

+2
Sep 12 '13 at 14:44
source share

To simplify this use,

 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 a for loop, use this:

  for (int iterator = 0; iterator < myList.Count; iterator++) { if (myList[iterator].Contains("String Pattern")) { return myList[iterator]; } } 
+1
Sep 12 '13 at 14:50
source share

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 the ability to "query" any collection of data. You can use the syntax as a database query (select, where, etc.) in a collection (here is a collection (list) of rows).

so what you do is: "Get me from the list where it satisfies the given condition"

inside where do you use lambda expression

to briefly tell a lambda expression, it's something like (input parameter => return value)

therefore, for the "item" parameter, it returns "item.Contains (" required string ")". Thus, it returns true if the element contains a string and, therefore, it is selected from the list because it satisfies the condition.

+1
Sep 12 '13 at 14:56 on
source share



All Articles