So I have a datatable which has many columns, but mainly at focus are two columns. “Associate ID” and “Effective Date”. I want to write a LinQ query to give me a datatable out of the existing by remove all the duplicates rows of same value of “Associate ID” but keeping only the latest row according to “Effective Date”. So suppose my Datatable looks like this:
It will delete the first row entry and give the rest of the datatble.
PS: Yes the effective date can have either MM-dd-yyyy or MM/dd/yyyy format.
So for this I have written the following code:
(From row In AdditionsReportDT.AsEnumerable()
Group row By associateID = row.Field(Of String)("Associate ID") Into Group = Group
Let maxDateRow = If(Group.Count() = 1,
Group.First(),
Group.OrderByDescending(Function(r As DataRow) DateTime.ParseExact(r.Field(Of String)("Effective Date"), {"MM-dd-yyyy", "MM/dd/yyyy"}, CultureInfo.InvariantCulture)).First())
Select maxDateRow).CopyToDataTable()
I am getting errors in this, please could someone help me, thanks in advance
Dim distinctRows = (From row In AdditionsReportDT.AsEnumerable()
Group row By associateID = row.Field(Of String)("Associate ID") Into Group
Let maxDateRow = If(Group.Count() = 1,
Group.First(),
Group.OrderByDescending(Function(r) DateTime.ParseExact(r.Field(Of String)("Effective Date"), {"MM-dd-yyyy", "MM/dd/yyyy"}, CultureInfo.InvariantCulture)).First())
Select maxDateRow).CopyToDataTable()
(From row In AdditionsReportDT.AsEnumerable()
Group row By associateID = row.Field(Of String)("Associate ID") Into Group = Group
Let maxDateRow = If(Group.Count() = 1,
Group.First(),
Group.OrderByDescending(Function(r As DataRow) DateTime.ParseExact(r.Field(Of String)("Effective Date"), {"MM-dd-yyyy", "MM/dd/yyyy"}, System.GlobalizationCultureInfo.InvariantCulture, DateTimeStyles.None)).First())
Select maxDateRow).CopyToDataTable()
Error ERROR Validation Error Compiler error(s) encountered processing expression “(From row In AdditionsReportDT.AsEnumerable()
Group row By associateID = row.Field(Of String)(“Associate ID”) Into Group = Group
Let maxDateRow = If(Group.Count() = 1,
Group.First(),
Group.OrderByDescending(Function(r As DataRow) DateTime.ParseExact(r.Field(Of String)(“Effective Date”), {“MM-dd-yyyy”, “MM/dd/yyyy”}, System.GlobalizationCultureInfo.InvariantCulture, DateTimeStyles.None)).First())
Select maxDateRow).CopyToDataTable()”.
‘GlobalizationCultureInfo’ is not a member of ‘System’.
Data type(s) of the type parameter(s) in extension method ‘Public Function OrderByDescending(Of TKey)(keySelector As System.Func(Of System.Data.DataRow, TKey)) As System.Linq.IOrderedEnumerable(Of System.Data.DataRow)’ defined in ‘System.Linq.Enumerable’ cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. Test.xaml
Yes you can keep the below code in assign @Biswas_Ishan
(From row In AdditionsReportDT.AsEnumerable()
Group row By associateID = row.Field(Of String)("Associate ID") Into Group = Group
Let maxDateRow = If(Group.Count() = 1,
Group.First(),
Group.OrderByDescending(Function(r As DataRow) DateTime.ParseExact(r.Field(Of String)("Effective Date"), {"MM-dd-yyyy", "MM/dd/yyyy"}, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None)).First())
Select maxDateRow).CopyToDataTable()
(From row In AdditionsReportDT.AsEnumerable()
Group row By associateID = row.Field(Of String)("Associate ID") Into Group = Group
Let maxDateRow = If(Group.Count() = 1,
Group.First(),
Group.OrderByDescending(Function(r As DataRow) DateTime.ParseExact(r.Field(Of String)("Effective Date"), {"MM-dd-yyyy", "M/dd/yyyy"}, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None)).First())
Select maxDateRow).CopyToDataTable()