Check which word contains the word from DT

My dt contains words like
chocolates
biscuits
ice cream

i get the string from as input (lets say strCustomerInput)

I need to check if strCustomerInput.contains(dt words) if yes i need to fetch which word from strCustomerInput contained the word from dt(lets say strMatchedWord) ,take that strMatchedWord and check if strMatchedWord is present in dt

How can i do this in linq .

dt_Badword.AsEnumerable().Any(Function(row) Str_message.ToLower.Contains(row.Field(Of String)(0).ToLower)) - this gives if strCustomerInput.contains(dt words)

How to proceed futher …

Hi @tharani.natarajan ,

As it a Retrieval with a Condition, we could use Where() method to fetch the matching rows and then perform the Select() on it.

dt_Badword.AsEnumerable().Where(Function(row) Str_message.ToLower.Contains(row(0).ToString.ToLower)).Select(Function(x)x(0).ToString).FirstOrDefault

Check if the above method works - If no match, it results in Nothing value.

But there are still confusions with the phrase …

lets assume

dt is
chocolate
ice cream
Candy

strCustomerInput - where is my ice cream

strMatchedWord will be ice cream (from strCustomerInput)

now check if ice cream is present in dt .

@tharani.natarajan ,

I believe we are already performing this with the First Stage, so maybe not needed. However, were you able to check the Expression provided above ?

Does it give out a result or Nothing or an Error ?

if my input text strCustomerInput = I work in Candyland"

dt is
chocolate
ice cream
Candy

this becomes true as Candyland contains candy.

so thought of logic

step 1 -check strCustomerInput contains dt if yes take the word that go matched from strCustomerInput and proceed to step 2
step 2 -keep the word that got matched in this case it is Candyland check in dt whether Candyland is present in dt .

@tharani.natarajan Try this then

dt.AsEnumerable.Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(str.ToLower,"\b" + x(0).ToString.Trim.ToLower + "\b")).Count>0

cheers

lets assume my dt contains ice cream and if str =“i want ice in my coke” it becomes true as ice is there .i want this to be false.i want exact match from dt .it works for single words in dt but doest work for multiple word though.

@tharani.natarajan

It would be false…as per the above code…because it searches for full ice cream in the given string…and not bits like ice or cream

The given formula above check if the value in dt is matchign with str and not viceversa and it is checking for full string that is present in dt and not partial

Cheers

1 Like

@tharani.natarajan ,

Adapting to Regex as mentioned by @Anil_G or the below modified Expression :

DT.AsEnumerable().Where(Function(row) Split(InputString).Any(Function(y)y.ToLower.Equals(row(0).ToString.ToLower))).Select(Function(x)x(0).ToString).FirstOrDefault

Debug Visuals (Both Cases) :
image

This is very helpful. can we get which value from dt got matched and store in a string ?

@tharani.natarajan

If count>0 then on the then side use

dt.AsEnumerable.Where(function(x) System.Text.RegularExpressions.Regex.IsMatch(str.ToLower,"\b" + x(0).ToString.Trim.ToLower + "\b"))(0) to get the matched string

Assuming only one will match…if multiple then can get all as well

cheers

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