Confused with Linq's Select

I have a list of string, and I want to get all elements that contain a certain string, and get a new list.
Say, myList has the following elements in this order:
(0): “faknknf X1 fnk”
(1): “grnk X2 gknr”
(2): “gnkew X1 hlmlw”
(3): “ifneic X1 lwcgrgs”
(4): “efkncaw X2 gknek”

Now, I want a new list OR it would be great if I could reuse the same list (=myList), that contains elements that contain “X1”. So the new list should be like

(0): “faknknf X1 fnk”
(1): “gnkew X1 hlmlw”
(2): “ifneic X1 lwcgrgs”

And I tried using Linq like the following:

myList.Select(Function(x) x.Contains(“X1”)).ToList

I am assigning this to another new list of string, but it says “Cannot convert IEnumerable (Of Boolean) into List(Of String)”. Why does it do this? I thought this code would select elements that match the condition…

Hi,

Select method returns value of the function.
If you want to filter the list, you should write the following

myList.Where(Function(x) x.Contains(“X1”)).ToList

Regards,

Yoichi

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.