LINQ query on Generic List

Hi Everyone,

I am having below generic list,

TestList = New List(of string)(new String(){“QUS0031056PR19A”,“US00073718PR20A”,“QHK0016342CA19A”})

Using LINQ, I want to get all strings in an another list which are of type xUSxxxxxxxxx19x. The first element in above list(QUS0031056PR19A) satisfy this pattern. It should be noted that all element are of same length(15 characters).

Any help on this request would be much appreciated.

@Kapil Check this Expression :

testList.Where(Function(x)Regex.IsMatch(x.Trim,".US.{9}19.")).ToList
2 Likes

1 Like

Thanks!! Its working

1 Like

Thanks! Its working

Hi @supermanPunch,

There is an update in requirement now. Instead of 19 in the pattern xUSxxxxxxxxx19x, I need to filter all string whose value is less than 19, like xUSxxxxxxxxx18x, xUSxxxxxxxxx17x, xUSxxxxxxxxx16x etc.

Is it possible to create linq query for this?

hi @ppr

There is an update in requirement now. Instead of 19 in the pattern xUSxxxxxxxxx19x, I need to filter all string whose value is less than 19, like xUSxxxxxxxxx18x, xUSxxxxxxxxx17x, xUSxxxxxxxxx16x etc.

Is it possible to create linq query for this?

@Kapil Check this Expression :

testList.Where(Function(x)Regex.IsMatch(x.Trim,".US.{9}[01][0-9].")).ToList
4 Likes

@Kapil

Great solution for this @supermanPunch! However i encountered that the value 19 is still included, but the question was less than 19.

I changed your solution a bit so that the 19 isn’t included anymore.

So the whole statement is

testList.Where(Function(x)Regex.IsMatch(x.Trim,".US.{9}(([01][0-8])|09).")).ToList

Best regards

Leo

4 Likes

Thanks everyone for your help!!!

3 Likes