Match string from DataTable without IF

I have DT1 with column names File & FileID

I have an argument “FileName” having string data, now I have to match using contains and get the matched FileID from the DT1.

e.g FileName.contains(File)

I want to achieve this without using the IF condition

Hi,

Can you try the following expression?

listString = DT1.AsEnumerable.Where(Function(r) FileName.contains(r("File").ToString)).Select(Function(r) r("FileID").ToString).ToList

Regards,

1 Like

it looks like work for me, but I need to use the string for the next action, how do I get that? as the output is in List of string.

String.join("",listString)

Hi,

Basically, we can use For Each activity as the following. (Because there is possibility to contains multiple items.)

img20210129-3

Regards,

1 Like

Okay.

But I am always looking for one output only.

Hi,

If it always contains one target item in DT1, we can use the following expression.

str =  DT1.AsEnumerable.Where(Function(r) FileName.contains(r("File").ToString)).Select(Function(r) r("FileID").ToString).Single

Regards,

thank you.

But what I am expecting is, If matched then there will be one result else there is always a chances of no match also.

Hi,

All right. Can you try the SingleOrDefault method as the following? It will returns null if no match.

str = DT1.AsEnumerable.Where(Function(r) FileName.contains(r("File").ToString)).Select(Function(r) r("FileID").ToString).SingleOrDefault

Regards,

Okay.

Will this be the correct way to proceed(below screenshot attached)?

Hi,

Which type is MatchedID, string or List<String>?
If it’s String type and you use SingleOrDefault Method, condition will be MatchedDT1 isnot Nothing

Regards,

1 Like

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