Final Edit: In fact the code given by ChatGPT works fine, the problem was further down the loop which processed the v_dtResult datatable and looped over the first row of the table. The real problem was therefore between the chair and the keyboard
so you can take the code from ChatGPT or from Yoichi, both work
Regards, Olivier
Hi All
I tried to do a linq query by myself but it didnât work so I asked chatGPT and it gave me something, but the result is not good. Here is the problem:
I have a datatable v_dtData as below (itâs a sample)
I want to group by PR, Order and Amount, then, order by Action Date Descending then by Affection Date descending and copy the first row of the group by in a new datatable
here is the code from ChatGPT:
v_dtResult = v_dtData.AsEnumerable() _
.GroupBy(Function(row) New With { _
Key .PR = row(âPRâ).ToString, _
Key .Commande = row(âOrderâ).ToString,
Key .Montant = convert.ToDouble(row(âAmountâ))
}) _
.Select(Function(Group) Group.First()) _
.OrderByDescending(Function(row) row.Field(Of DateTime)(âaction dateâ)) _
.ThenByDescending(Function(row) row.Field(Of DateTime)(âaffectation dateâ)) _
.CopyToDataTable()
the problem is that every row of the v_dtResult are identical, the query only the first group by is processed and repeated until the end. ChatGPT tells me something about class Anonymous and ask me to change for:
Public Class GroupKey
Public Property PR As String
Public Property Commande As String
Public Property Montant As Double
End Class
v_dtResul = v_dtData.AsEnumerable() _
.OrderByDescending(Function(row) row.Field(Of DateTime)(âaction dateâ)) _
.ThenByDescending(Function(row) row.Field(Of DateTime)(âaffectation dateâ)) _
.GroupBy(Function(row) New GroupKey With { _
.PR = row(âPRâ).ToString(), _
.Commande = row(âorderâ).ToString(), _
.Montant = Convert.ToDouble(row(âamountâ)) _
}) _
.Select(Function(Group) Group.First()) _
.CopyToDataTable()
but I canât manage how to implement that in an assign
Can somebody help me ?