Linq Query to check no datarow

Hi all,

I’m new to LINQ query and I received a solution previously based on my query from this post.

https://forum.uipath.com/t/compare-two-excels-and-update/284730/4?u=jenkim

So I encountered an error whenever there’s no data to be copied to datatable. So I change it as below to cater that error but seems like it doesn’t work with those variable type.

I change it to - FilteredRows as System.Collections.Generic.IEnumerable<System.Data.DataRow>:

Linq query:

FilteredRows = (From d In MainDT.AsEnumerable
Group d By k=d("UPDATED_BRN").ToString.Trim Into grp=Group
Let check = grp.Any(Function ( r ) CheckOrderDT.AsEnumerable.Any(Function (x) x("ACCNT_ID").ToString.Trim.Equals(r("ROW_ID").ToString.Trim )))
From g In grp
Let ra = New Object(){g(0),g(1),g(2),g(3),g(4),g(5),g(6),g(7),g(8),g(9), If(check, "Available",g(10))}
Select ResultDT.Rows.Add(ra))

ResultDT = If(FilteredRows.Any, FilteredRows.CopyToDatatable, MainDT.Clone)

Hope anyone can help me on this. Very much appreciated.

can you check following:

If(FilteredRows.Count()>0, FilteredRows.CopyToDatatable, MainDT.Clone)

Unfortunately, I still got the same error which is not able to assign to variable.

Can not assign ‘If(FilteredRows.Count()>0, FilteredRows.CopyToDatatable, MainDT.Clone)’ to ‘ResultDT’.

what about CheckOrderDT, has it content?

A common strategy is as following:

  • debug and inspect all variables / datatables
  • set breakpoint and test parts of the LINQ in the watch box / immediate panel
  • decompose the LINQ by
(FilteredRows = (From d In MainDT.AsEnumerable
Group d By k=d("UPDATED_BRN").ToString.Trim Into grp=Group
Select grp.toList).toList (returns a List(list(of Datarow) 

outer list=groups, inner list the group members

  • process groups and members within a nested for each
  • for each group -grp
    • do group actions
    • for each member - mbr
      • do memebr action

Currently what I did was, I check first CheckOrderDT for any data. If so then only do the LINQ query.

But what I noticed was, there will be an error if no match value found hence the CopyToDataTable is empty.

in general the Any should also work ith exceptions on empty datatable or if no rows are matching the condition

grafik

However just do the decomposition of the LINQ as we can jump out from the black box

Hello @ppr, sorry I was not clear on your instructions. Do you mean I need to separate the code?

Group the data in the column

From d In MainDT.AsEnumerable
Group d By k=d("UPDATED_BRN").ToString.Trim Into grp=Group

Check match value

Let check = grp.Any(Function ( r ) CheckOrderDT.AsEnumerable.Any(Function (x) x("ACCNT_ID").ToString.Trim.Equals(r("ROW_ID").ToString.Trim )))

Add to datarows

From g In grp
Let ra = New Object(){g(0),g(1),g(2),g(3),g(4),g(5),g(6),g(7),g(8),g(9), If(check, "Available",g(10))}
Select ResultDT.Rows.Add(ra))

Do you mean it like this?

@jenkim
find starter help here. I will come back in a little time and will do some introductions on the code:
GroupBy_SetGroupStatus_ByRefListMatch-Decomp.xaml (16.7 KB)

1 Like

Thank you so much @ppr for this. :smile:

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