Filter by linq

I want to filter datatable by linq
condition count character column Create ID <6 as below.

dt_input.AsEnumerable().Where(Function (row) row((“Create ID”)<“7”).Length.ToString).CopyToDataTable

error as below.

Please guide me for solve it.
Thank you

Hi @fairymemay

Try this:

filteredRows = dt_input.AsEnumerable().Where(Function(row) row.Field(Of String)("Create ID").Length < 6).CopyToDataTable()

filteredRows is of DataType System.Data.DataTable.

Hope it helps!!

Hi @fairymemay

Check the below linq expression,

dt_input.AsEnumerable().Where(Function (row) row("Create ID").toString.Length < 6).CopyToDataTable

Hope it helps!!

Hi @fairymemay

dt_Input.AsEnumerable().Where(Function(row) row("Create ID").ToString.Length <= 7).CopyToDataTable

Cheers!!

@lrtetala @mkankatala @parvathy

after run error as below.
Assign: The source contains no DataRows.

Sometime data after filter don’t have data.
How to manage it.

Hi @fairymemay

filteredRows = dt_input.AsEnumerable().Where(Function(row) row.Field(Of String)("Create ID").Length < 6)

filteredRows is of DataType IEnumerable(System.Data.DataRow)

=> Use an if condition and give like below

If 
   filteredRows.Any()
Then
     dt_Output = filteredRows.CopyToDataTable()
Else
    dt_Output = New System.Data.DataTable()
End If

dt_Output is of DataTpye System.Data.DataTable.

Hope it helps!!

1 Like

Okay @fairymemay

In that case use If condition then give the below expression,

dt_input.AsEnumerable().Where(Function (row) row("Create ID").toString.Length < 6).CopyToDataTable.rows.Count>0

→ In then block take assign activity to store in Output Datatable.

- Assign -> OutputDatatable = dt_input.AsEnumerable().Where(Function (row) row("Create ID").toString.Length < 6).CopyToDataTable

Hope it helps!!

1 Like
1 Like

Hi,
Use below query

Dim filteredRows As DataTable = dt_input.AsEnumerable() _
.Where(Function(row) row.Field(Of String)(“Create ID”).Length < 6) _
.Select(Function(row) row.Table.Clone()) _
.DefaultIfEmpty(dt_input.Clone()) _
.FirstOrDefault()

1 Like

@mkankatala same error as below.

First check in immediate panel that dt_Input have any datarows there or not… @fairymemay

@mkankatala yes, dt_input have data as below.

It was showing only Column not showing datarows, Could you check rows count of dt_Input in Immediate panel once.

dt_Input.Rows.Count

Error also showing there is no datarows in the Source, here source is dt_Input.

Hope you understand!!

@mkankatala dt_input.Rows.count = 2

image

Okay @fairymemay

Then do one thing, Before If activity, Take an assign activity and create a boolean datatype variable called Bool_Flag.

- Assign -> Bool_Flag = If(dt_input.AsEnumerable().Where(Function (row) row("Create ID").toString.Length < 6).CopyToDataTable.rows.Count > 0, True, False)

→ After Assign activity, insert If condition and give the below condition,

If Condition -
Bool_Flag = True

Then block
- Assign -> OutputDatatable = dt_input.AsEnumerable().Where(Function (row) row("Create ID").toString.Length < 6).CopyToDataTable

Else block
There is no rows in the OutputDatatable

Check the below workflow for better understanding,

Hope it helps!!

@mkankatala same errror.

Can you share your flow?

Could you share your Input excel file if it doesn’t have confidential information… @fairymemay

@mkankatala file input as attached.
input.xlsx (8.5 KB)

Okay @fairymemay I got it,

In the assign activity that we have used after Read range workbook activity change the expression as below,

- Assign -> Bool_Flag = If(dt_input.AsEnumerable().Any(Function (row) row("Create ID").toString.Length < 6), True, False)

It’s working fine now.

Hope it helps!!

1 Like

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