Güven Acar
Güven Acar

Reputation: 121

How can i change multiple same values with other in list?

I have a list

          List1 = {"abc", "abc", "dfg", "abc"}

i want change all "abc" values with "cde"

          List1 = {"cde", "cde", "dfg", "cde"} 

How can i change them with linq?

Thanks.

Upvotes: 0

Views: 37

Answers (1)

Blackwood
Blackwood

Reputation: 4534

Yes, you can use the Select method to check each member of the list and replace it. The following gives the result you expect.

Dim List1 As New List(Of String) From {"abc", "abc", "dfg", "abc"}    
List1 = List1.Select(Function(s) If(s = "abc", "cde", s)).ToList

If List1 is actually an array rather than a List, the method is similar.

Dim List1() As String = {"abc", "abc", "dfg", "abc"}
List1 = List1.Select(Function(s) If(s = "abc", "cde", s)).ToArray

Upvotes: 2

Related Questions