Hi all,
How can I remove duplicate rows only if column Time Off Quantity = 1 or -1 and retain all the other remaining columns as it is
Can this be done using linq query?
(From d In yourDataTableVar.AsEnumerable
Group d By k1=d(0).toString.Trim, d(1).toString.Trim Into grp=Group
Let setNonAbsOne = grp.Where(Function (f1) Not Math.Abs(CDbl(f1(2).toString.Trim)).Equals(1.0))
Let setAbsOne = grp.Except(setNonAbsOne, DataRowComparer.Default).Take(1)
From g In setNonAbsOne.Concat(setAbsOne)
Order By yourDataTableVar.Rows.IndexOf(g)
Select r = g).CopyToDataTable
thank you. i actually have more columns in the actual datatable and i’d need to group by 3 columns. what is wrong with the below query? i’m getting error “Range variable Trim is already declared”
Group d By k1=d(0).toString.Trim, d(1).toString.Trim, d(5).toString.Trim Into grp=Group
there’s also another issue because the query is checking Math.Abs(1), so it removes both 1 and -1 which is not duplicate as below. it should only remove when there’s another same value of 1/-1 found for the same date
remove duplicates will remove all duplicates, i only need to remove duplicates if column Time Off Quantity = 1 or -1 and remain the other rows. i’ve already stated it above