I have a datatable that has 6 columns: “Process”, “State”, “Folder process”, “Start time”, “End time”, “Host name (VM)”.
What I need is to stay with the most recent data that has the same “Process”, “State”, “Folder process”. The column I have is “Start time” and “End time”, the idea would be to use one of the two and filter so that it leaves me the most recent data.
In the image I show you an example, so that you better understand what I mean:
I tried to do it with Linq, but I have very little knowledge so it didn’t work and it’s wrong, I still put the code. There could be many lines, that’s why I use Linq.
out_dtDataFilteredJobs =
(From row In out_dtAllDataJobs.AsEnumerable
Group row By k=row("Process").ToString.Trim, k2=row("State").ToString.Trim, k3= row("Folder process").ToString.Trim ,k4=row("End time").ToString.Trim, k5=row("Host name (VM)")
Into grp=Group
Let startDate = grp.OrderBy(Function (r) DateTime.ParseExact(r("Start time").toString.Trim,"dd/MM/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture)).First
Select out_dtDataFilteredJobs.Rows.Add({k, k2, k3, startDate, k4, k5})
).CopyToDataTable
(From row In out_dtAllDataJobs.AsEnumerable
Group row By k=row("Process").ToString.Trim, k2=row("State").ToString.Trim, k3= row("Folder process").ToString.Trim , k5=row("Host name (VM)")
Into grp=Group
Select grp.OrderByDescending(Function (r) DateTime.ParseExact(r("Start time").toString.Trim,"d/M/yyyy H:m",System.Globalization.CultureInfo.InvariantCulture)).First()
).CopyToDataTable