How to check if the column in datatable contains specific value without loop

I have a datatable

For example:

GroupName. Age

ABC. 12

DCB. 14

I have to find if the column. GroupName has DCB. without loop

Use DT.AsEnumerable.Select(Function(x) X(“GroupName”).ToString.Contains(“DCB”)).Count

Which will return no. of rows where condition matches, you can further use it to do actions.

in Addition to:

hasFound =
YourDataTableVar.AsEnumerable.Any(Function (x) x(“GroupName”).toString.Trim.Equals(“DCB”))

we are using the Any as we want to give a chance to abort the processing, once the first occurence is found. We use this technique instead of Where(…).Count… as mentioned here:

CA1827: Do not use Count/LongCount when Any can be used (code analysis) - .NET | Microsoft Learn

1 Like

@sruthesanju

You can use filter datatable and check the outputdt.Count

cheers

You can’t do it without a loop. It’s a datatable. You have to loop through the rows to do things. Don’t let LINQ nor any expression like dt.asenumerable fool you - they’re still looping.

And don’t fall for the myth that For Each Row in Datatable is automatically slower than other methods.

@postwick

Without loop is wrong I guess…As Filter Datatble can be done with the given value…Which does not use loop at all

Later I would agree but relatively little faster with huge data

cheers

Of course Filter Datatable loops. It’s a datatable. Everything has to loop to do things to the rows. It just hides it from you.

1 Like

@postwick

But I guess using a for loop and if condition might not be same as using a filter