Linq query to extract data length which is equals to 12

I have a DataTable with multiple DataRow entries, and I need to extract the DataRow where the value in the ‘EAN/UPC’ column has a length of 12 characters

Hi @Sneha_1992

→ Use the Read range workbook activity to read the excel and store in a datatable called Input_dt.
→ You can use the LINQ Expression to filter the required data from the datatable,

- Assign -> OutputData = Input_dt.asenumerable.where(Function(X) (X("Column name").toString.length).equals(12)).Select(Function(Y) Y("Column name")).Firstordefault.toString

The OutputData is the string variable where you it will store the data which has length 12.

Hope it helps!!

What if we dont have value which is of length 12

@Sneha_1992

resultRow = dt.AsEnumerable().Where(Function(row) row("EAN/UPC").ToString().Length = 12).FirstOrDefault()

I have tried this and its not working

@Sneha_1992
You want the first occurance datarow or what ever datarows which exceeds 12 length.Please specify the requirement

resultRow = dt.AsEnumerable().FirstOrDefault(Function(row) row("EAN/UPC").ToString().Length = 12)

Then try this

Then try the below one,

- Assign -> Output = If(Not(String.isNullorEmpty(Input_dt.AsEnumerable().Where(Function(X) X("Column name").ToString().Length.Equals(12)).Select(Function(Y) Y("Column name")).FirstOrDefault().ToString())), Input_dt.AsEnumerable().Where(Function(X) X("Column name").ToString().Length.Equals(12)).Select(Function(Y) Y("Column name")).FirstOrDefault().ToString(), "No data") 

If not data found with length 12 then it will store the No data in the Output variable

Hope it helps!!

1 Like

Amazing!!! very quick response

1 Like

It’s my pleasure… @Sneha_1992

Happy Automation!!

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