I have a datatable read from an excel file which can or cannot have same column names. I want to rename those columns to something else like column_1,etc. and then make this new column as header.
1 Like
We can model like the following:
NewColNamesInfo =
(From t In dtData.Rows(0).ItemArray.Select(Function (x,i) Tuple.Create(i,x.toString))
Let t2 = If(String.IsNullOrEmpty(t.Item2.toString.Trim), Tuple.Create(t.item1, "COL"),t)
Group t2 By k=StrConv(t2.Item2.toString, VbStrConv.ProperCase).Trim.Replace(" ","") Into grp=Group
Let cg = grp.Select(Function (x,i) Tuple.Create(x.Item1, (k & "_" & i.toString).Replace("_0",""))).toList
Select cg).SelectMany(Function (x) x).OrderBy(Function (ot) ot.Item1).toList
item in NewColNamesInfo
dtData.Columns(item.Item1).ColumnName =
item.Item2
Above LINQ is currently handling more e.g. ProperCase …
But it will handle also the adress, adress_1 … cases as it append _Counter on the duplicated names
Getting the following error when executing your workflow in the assign activity-
Assign: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
thanks jayesh although I cannot use external packages or dependencies!
You should make a new post instead of digging up a 3 year old post.