Hi,
how can I remove first 4 char from below data table using query
I would like to remove all chracter starting with “740_” in invoiceno column and get the output as
GM-XXX for all data in datatable
I did try to use below command but did not get the output
queryremove4char=DTPi.AsEnumerable().Orderby(Function(a) a.Field(of string)(“invoiceno”).ToString.Replace(“740_”,” ”) ).CopyToDatatable()
Thank you
Regards
Jamuri
postwick
(Paul Ostwick)
September 8, 2021, 1:38pm
2
Get cell value
Replace 740_ with “”
Write cell value
OR
Read sheet to dt
loop through dt
Replace value and write cell
prasath_S
(prasath S)
September 8, 2021, 1:38pm
3
Hi @Abang_Jamuri_Abang_Shoker
Please try below steps,
Read the excel sheet data in the datatable variable name dt .
Drag and drop the invoke code activity.
In edit arguments, create a variable dt , set direction as in/out and pass value as dt .
In edit code type the following ,
dt.asenumerable.where(function(row) row(“invoiceno”).tostring.startswith(“740_”)).tolist.foreach(sub(row) row(“invoiceno”)= row(“invoiceno”).tostring.replace(“740_”,“”))
Thanks
1 Like
postwick
(Paul Ostwick)
September 8, 2021, 1:40pm
4
Why overcomplicate it with Invoke Code? Just use an Assign inside a For Each to change the value in each row.
1 Like
prasath_S
(prasath S)
September 8, 2021, 1:41pm
5
It will reduce the numer of activities to use and will done in single activity.thats why
ppr
(Peter Preuss)
September 8, 2021, 1:41pm
6
we do see a LINQ which is ordering on invoiceno by not using begining 740_
Which output you got? Validation message, not right ordered… Please tell us details
ghazanfar
(Ghazanfar Ali)
September 8, 2021, 1:43pm
7
Hi,
dt = dt.AsEnumerable().Where(Function(a) a.Field(of string)(“yourcolumnname”).ToString.Replace(“your old value”,” your new value”).ToString).CopyToDatatable()
hope this will work
1 Like
postwick
(Paul Ostwick)
September 8, 2021, 1:54pm
8
It’s easier to follow an automation that uses regular activities, even if it’s 2 or 3 instead of 1. When writing automations you have to consider that others will have to maintain it in the future, so writing code that’s easy to understand is very important.
1 Like
prasath_S
(prasath S)
September 8, 2021, 1:58pm
9
Yes @postwick you are correct when we deal with a simple workflow,but when comes to large workflows which already got a huge number of activities in it, the more number of activities we use the workflow will become more complex and difficult to maintain in the future.
Though it’s my perspective, thanks for the suggestion will remember that in future.
Thanks
J0ska
September 8, 2021, 2:16pm
10
This code is invalid.
Where() method is used to filter dt, not transform.
Following code could be used in an assign statement.
dt2 = dt1.AsEnumerable().Select(Function(row) dt2.rows.add(row("col1").ToString.Replace("from","to"))).CopyToDatatable()
But IMHO best approach is proposed by @prasath_S
Cheers
1 Like
Hi Prasath
I got the error message
Invoke code: Exception has been thrown by the target of an invocation.
ppr
(Peter Preuss)
September 8, 2021, 2:24pm
12
With try catch the invoke code and exception.InnerException.Message you can get details on the exception
it looks also that the DataTableVar was not passed, so we expect a reference not set exception.
But the more important thing is:
Your initial LINQ was doing a particular ordering
this LINQ is doing a filtering / replace
What you want to achieve?
prasath_S
(prasath S)
September 8, 2021, 2:24pm
13
Pass the dt variable and make sure the column name matches with the excel.
I use invoiceno because that was my column name,use with the one you have.
Thanks
still no changes on the data “740_” still exist
prasath_S
(prasath S)
September 8, 2021, 2:30pm
15
Try this code I can see that you hae. dtpier has variable name instead of dt.
dtPier.asenumerable.where(function(row) row(“invoiceno”).tostring.startswith(“740_”)).tolist.foreach(sub(row) row(“invoiceno”)= row(“invoiceno”).tostring.replace(“740_”,“”))
Thanks
Reconcile_removechar.xaml (8.9 KB)
tests.xlsx (8.6 KB)
still did not change
1 Like
postwick
(Paul Ostwick)
September 8, 2021, 2:57pm
17
See how much trouble it is to help them get this code correct?
Activities would be easier.
prasath17
(Guru)
September 8, 2021, 2:58pm
18
@Abang_Jamuri_Abang_Shoker - please check this…
dtInput.AsEnumerable().Where(Function(row) row(2).ToString.StartsWith("740_")).ToList.ForEach(Sub(row) row(2) = row(2).tostring.replace("740_",""))
You can change row(2) with row(“invoiceno”)
If you don’t have large # rows then you can easily do the conventional method suggested by @postwick
1 Like
Thanks prasath17,
manage to get the output. I need write it back to the file. my mistake did not realize that.
thanks again for your help.
I need this method of query because it involve more than 100 lines that need to edited.
Thank you
prasath_S
(prasath S)
September 8, 2021, 3:32pm
20
Yes @postwick activities are easier I agree ,but this is the best i can think of both time saving as well as minimize the complexity of project.
Glad that worked in the end.
Thanks