Linq Query to Filter datatable - Contains

I have a DT with the following values

CompanyName Date
Test.co.in 12
Raindom 15
Rainbow 17

I want to fiter the above table with the value “Test”
How I can do that?

Dt.AsEnumerable.Where(function(r) r(column).Equals(“Test”)).Count>0 command will not work as it is not exact match…
Can someone help me with the Contains logic for linq query to get the first row

@Pinky_AG

Check below for your reference

dt_Data = dt_Data.AsEnumerable().Where(Function (row) row(“CompanyName”).ToString.ToUpper.Trim.Contains(“Test”)).CopyToDataTable.Rows.Count > 0

Hope this may help you

Thanks,
Srini

when it is about getting the first data row:

myDataRow =

Dt.AsEnumerable.FirstOrDefault(function(r) r("CompanyName").toString.ToUpper.Contains("TEST"))

I am using this condition in the if Condition which will return Boolean value, so if we use “tostring”,it will not work and throw error… And Column name and value are dynamic .

HI,

If you need to use the above expression in IF condition, Any method will help you. Can you try this?

in_dt_Files.AsEnumerable().Any(Function(r) r(vColumnName2).ToString().ToLower()=r(vColumnValue1).ToString().ToLower())

Regards,

1 Like

Thank you…this is working for (=) condition which is exact match. I am lookinh for Contains condition. Can you please help

Hi,

How about the following?

in_dt_Files.AsEnumerable().Any(Function(r) r(vColumnName2).ToString().ToLower().Contains(r(vColumnValue1).ToString().ToLower()))

Regards,

Getting this error… count is not a member of boolean


Even copyToDataTable function is not there

Hi,

It’s unnecessary to add .Count>0. Please input the above as it is. This returns boolean.

image

Regards,

Yes, just got that, thank you. It is working

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