Check length of column values

Case 1:
image

dt_Test.Select("[Column1] = ‘1’ ").Length.ToString

Output/result = “0”

Case 2:
image

dt_Test.Select("[Column1] = ‘1’ ").Length.ToString

Output/result = “2”

Why doesnt Case 1 output “1”?

can you try LINQ?

dt_Test.AsEnumerable.Where(Function (x) x(0).toString.Trim.Length = 1).Count

maybe a space… will confuse. You can also check for the trim function when using the search expression for SELECT

I am trying to check if there is any “1” in “Column1”.

If there isnt any “1” I want to throw a error.

dt_Test.AsEnumerable.Where(Function (x) x(“Column1”).toString.Trim.Length = 1).Count

This LINQ query currently outputs how many datarows there is.

sorry: dt_Test.AsEnumerable.Where(Function (x) x(0).toString.Trim.Equals(“1”)).Count

1 Like

also a good pointer for using the Any / All Operator:

dt_Test.AsEnumerable.All(Function (x) x(0).toString.Trim.Equals("1"))

1 Like

Perfect it works great, LINQ is really nice, need to get better with it :slight_smile:

So what is the difference with using All/Any instead of where in this case?

Also what is the way to make the SELECT method work in this case, you mentioned trim?

All/Any will return a boolean
e.g. All LINQ Sample

not sure bit give a try on:

dt_Test.Select("TRIM([Column1]) = '1' ").Length.ToString

1 Like

Oh okay, thanks nice to know :slight_smile:

Got this, maybe some formatting issue.
Log Message: Type mismatch in function argument: Trim(), argument 1, expected System.String.

was working. Find starter help here:
Select_TrimMethod.xaml (6.4 KB)

1 Like

Nice, just checked my column data type, its System.Object

So I would need to convert it first I think.

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