-4

Just wandering how can I find value from List if array value contains string in c# .net?

Example: I have the following code to stored the value into the List

var tempTraker = new List<string>();
tempTraker.Add("2|a");
tempTraker.Add("1|e");
tempTraker.Add("4|r");
tempTraker.Add("3|h");

How can I check tempTraker array value contains "1|" and return the full value "1|e" ? Will it be possible?

0

2 Answers 2

1
var value = tempTraker.FirstOrDefault(s => s.Contains("1|"));
Sign up to request clarification or add additional context in comments.

1 Comment

One good thing to note is that this code will get the first item in the list that contains that value, or if nothing in the list does it will return null. So you will probably want to check for null after using this.
0

You could also use the below if you just wanted to get a bool value.

tempTraker.Any(x => x.Contains("1|"));

Edit: @Maxium's answer is correct. I missed the part where they wanted the value returned.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.